我正在尝试创建一个Excel VBA脚本,它将输入2列作为输入并将值输出到文本文件中。
e.g
A1:123 B1:大道
A2:231 B2:另一条车道
我在txt文件中的输出后看起来像这样:
门牌号码:123街道名称:大道
----
门牌号码:231街道名称:另一条车道
----
基本上我正在寻找的是: “门牌号码:”+ A1 +“街道名称:”+ B2 “------”
我只用了一列就可以使用它但是我坚持添加第二列。
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
任何帮助将不胜感激
答案 0 :(得分:1)
你可以用这个去多个方向,一种方法是创建一个bcolumn变量并将其设置为你行中的下一个单元格索引:
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
您的输出可能需要采用不同的格式,但这应该可以帮助您实现目标。为了一点可重用性,您可以设置函数来获取这些字符串的数组。