使用VBA将每一列导出到唯一的txt文件

时间:2018-08-04 16:42:38

标签: excel vba excel-vba loops

我一直在努力跟踪将汤姆·乌尔蒂斯({3}}的导出数据导出到txt文件,他将每一行提取到一个唯一的txt文件中。 汤姆斯代码:

Sub TextExport()
Dim strPath$, x&, i%, txt$
strPath = ThisWorkbook.Path & "\"
For x = 1 To Cells(Rows.Count, 1).End(xlUp).Row 'From row 1 to last row
Open strPath & Cells(x, 1).Value & ".txt" For Output As #1
txt = ""
For i = 1 To 3 'From Column 1 to 3
txt = txt & Cells(x, i).Value & vbTab
Next i
Print #1, Left(txt, Len(txt) - 1)
Close #1
Next x
MsgBox "The text files can be found in " & strPath & ".", 64, "Complete"
End Sub

我想将每一列而不是每一行提取到一个txt文件中。

1 个答案:

答案 0 :(得分:1)

经过几次调整,我设法实现了想要的目标。 第一行将是输出txt文件的标题。

Sub TextExportCol()
Dim strPath$, x&, i%, txt$
strPath = ThisWorkbook.Path & "\"
For x = 1 To Cells(1, Columns.Count).End(xlToLeft).Column 'Loop from  column 1 to end column
    Open strPath & Cells(1, x).Value & ".txt" For Output As #1
    txt = ""
        For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row 'How many rows to include, from row 1 to end
        txt = txt & Cells(i, x).Value & vbNewLine
        Next i
    Print #1, Left(txt, Len(txt) - 1)
    Close #1
    Next x
MsgBox "The text files can be found in " & strPath & ".", 64, "Complete"
End Sub