繁体   English   中英

Excel VBA将多列输出到文本文件

[英]Excel VBA output multiple columns to text file

我正在尝试创建一个Excel VBA脚本,该脚本将2列作为输入并将其值输出到文本文件中。

例如

A1:123 B1:大道

A2:231 B2:另一条车道

我正在寻找类似这样的txt文件中的输出:

门牌号:123街道名称:avenue

----。

门牌号:231街道名称:另一条车道

----。

基本上我正在寻找的是:“门牌号:” + A1 +“街道名称:” + B2“ ------”

我只用1列就可以使用它,但是我不得不增加2列。

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

任何帮助,将不胜感激

您可以为此进行很多指导,一种方法是创建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

您的输出可能需要采用不同的格式,但这应该可以使您步入正轨。 为了提高可重用性,您还可以设置函数以接受这些字符串的数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM