Excel VBA将多列输出到文本文件
我试图创build一个Excel VBA脚本,将需要2列作为input,并输出到文本文件的值。
例如
A1:123 B1:大道
A2:231 B2:另一个车道
我在一个txt文件中的输出看起来像这样:
门牌号码:123街道名称:大道
—-。
门牌号码:231街道名称:另一个车道
—-。
基本上我在找的是:“门牌号码:”+ A1 +“街道名称:”+ B2“——”
我有这个工作只有1列,但我坚持添加一秒。
Sub CreatetestScript() If Dir("C:\test.txt") <> "" Then Kill ("C:\test.txt") End If Sheets("test").Select Dim acolumn As String r = 2 Do While Cells(r, 1) <> "" acolumn = Cells(r, 1).Value run (Cells(r, 1)) r = r + 1 Loop End Sub Sub run(ByVal acolumn As String) Dim fileName As String fileName = "C:\test.txt" Open fileName For Append As #1 Print #1, "" Print #1, "House Number = " + acolumn Print #1, "--------------" Close #1 End Sub
任何帮助,将不胜感激
你可以用这个方法去多个方向,一种方法是创build一个bcolumnvariables,并将其设置为行中下一个单元格的索引:
Sub CreatetestScript() If Dir("C:\test.txt") <> "" Then Kill ("C:\test.txt") End If Sheets("test").Select Dim acolumn As String Dim bColumn As String r = 2 Do While Cells(r, 1) <> "" acolumn = Cells(r, 1).Value bColumn = Cells(r,2).value run (Cells(r, 1), Cells(r,2)) r = r + 1 Loop End Sub Sub run(ByVal acolumn As String, ByVal Bcolumn As String) Dim fileName As String fileName = "C:\test.txt" Open fileName For Append As #1 Print #1, "" Print #1, "House Number = " + acolumn Print #1, "--------------" Print #1, "," Print #1, "Street = " + Bcolumn Print #1, "--------------" Close #1 End Sub
您的输出可能需要格式不同,但这应该让你正确的轨道上。 为了一点可重用性,您可以设置函数来获取这些string的数组。