错误types不匹配 – 如何从列A中的所有数据创build.txt文件
我一直在这行代码中得到types不匹配的错误。
stream.WriteLine CellData
下面的完整代码
Sub Write_to_File() Dim FilePath As String Dim CellData As Variant Dim LastCol As Long Dim LastRow As Long Dim fso As FileSystemObject Set fso = New FileSystemObject Dim stream As TextStream ' Create a TextStream. Set stream = fso.OpenTextFile("C:\...\test_labs.txt", ForWriting, True) var3 = Worksheets("label").Columns(1) CellData = "" CellData = var3 stream.WriteLine CellData stream.Close MsgBox ("Done") End Sub
我试图让表1中的所有列A写入文本文件。 这个代码从表2中的一个命令button运行。我还是VBA新手,所以任何帮助表示赞赏。
谢谢
下面的工作,但我不会推荐你把整个列转储到一个文件,可能需要一些时间。 最好将其限制为非空行,或者可能find最后使用的单元格,并且只输出到那一点?
' Create a TextStream. Set stream = fso.OpenTextFile("K:\test_labs.txt", ForWriting, True) For Each CellData In Worksheets("label").Columns(1).Cells stream.WriteLine CellData Next stream.Close MsgBox ("Done") End Sub
以下部分将查找A列中填充的最后一行(将其存储在您已经声明的LastRow
variables中)并停在那里:
' Create a TextStream. Set stream = fso.OpenTextFile("K:\test_labs.txt", ForWriting, True) With Worksheets("label") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For Each CellData In .Range("A1:A" & LastRow).Cells stream.WriteLine CellData Next End With stream.Close MsgBox ("Done") End Sub
有关信息:您首先得到错误的原因是因为stream.WriteLine
需要一个标量String
input参数,但是这一行:
var3 = Worksheets("label").Columns(1)
将var3
设置为一个数组。