我在VBA中的這個函數有問題,它將某些列保存到文本文件中。 它只保存列B的值,並忽略另一個for循環,其中我循環列C的值。我試圖添加直接寫入文件 - > objTextStream.writeline(「COPY THIS STRING」)只是要查看問題出在哪裏,但仍然複製此STRING仍然沒有保存在文本文件中。VBA文件系統對象
什麼似乎是問題。先謝謝你們。
Sub SaveToTxt(toRange As Integer)
Dim MyRange As Range
Dim MyRange2 As Range
Dim objFileSyatem As Object, objTextStream As Object
Dim cell As Range
Set MyRange = ActiveSheet.Range _
("B1:B" + CStr(toRange) & Cells(Rows.Count, 1).End(xlUp).Row)
Set MyRange2 = ActiveSheet.Range _
("C1:C" + CStr(toRange) & Cells(Rows.Count, 1).End(xlUp).Row)
'create filesystem and textstream objects, the name of the .txt file
'is one of parameters of CreateTextFile method
Set objFileSyatem = CreateObject(_
"Scripting.FileSystemObject")
Set objTextStream = objFileSyatem.CreateTextFile(_
"C:\Users\wmeniola\work work work\Ranges deletion\test.txt", True)
'loop through all cells in MyRange and save contents as
'separate lines in newly created .txt file
For Each cell In MyRange.Cells
objTextStream.writeline (cell.Value)
Next
For Each cell In MyRange2.Cells
objTextStream.writeline (cell.Value)
Next
objTextStream.writeline ("COPY THIS STRING")
objTextStream.Close
End Sub
首先我覺得你應該寫' objTextStream.writeline「COPY THIS STRING」'without() – Noldor130884