【问题标题】:VBA to loop through text delimited files in a folder and export as csvVBA循环遍历文件夹中的文本分隔文件并导出为csv
【发布时间】:2018-02-05 18:06:29
【问题描述】:

需要帮助找出我的代码不循环的原因 我的文件夹。它一遍又一遍地循环同一个文本文件。当我 中断并运行代码,似乎下一个文件在行 是正确的,但它会打开以前的文件。

     Sub MikesMacro()

     Dim strFile As String
     Dim intNumberOfFiles As Integer
     Dim wbText As Excel.Workbook
     Dim path As String

     path = "C:\Users\MStarks\Desktop\Cincy Data Edits\PULSE IMPORTS\"

     strFile = Dir(path & "*.TXT")

         Do While Len(strFile) <> ""

         Workbooks.OpenText Filename:=(path & "*.TXT") _
         , DataType:=xlDelimited, Tab:=True, FieldInfo:=Array(Array(1, 1), _
         Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1))

         Set wbText = ActiveWorkbook

         'Starts the macro used to set up format the Telog likes
         'Macro not included
         'start save as .CSV cycle

     strFile = Dir

     Loop

     End Sub

【问题讨论】:

  • Filename:=(path &amp; "*.TXT") 在 while 循环中,并且路径的值永远不会被更新。请改用strFile。我什至想知道公开声明是否有效。
  • 还可以考虑使用indenter 以使您的代码更易于遵循。
  • 需要从Filename:=(path &amp; "*.TXT") 中删除.TXT 并添加到路径和strFile。所以最终的代码是Filename:=(path &amp; strFile)

标签: excel vba csv export-to-csv do-while


【解决方案1】:

这个怎么样?

Sub LoadPipeDelimitedFiles()
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\*.txt")
    Do While xFile <> ""
        xCount = xCount + 1
        Sheets(xCount).Select
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
          & xStrPath & "\" & xFile, Destination:=Range("A1"))
            .Name = "a" & xCount
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
            xFile = Dir
        End With
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox("no txt files")
End Sub

https://www.extendoffice.com/documents/excel/3388-excel-import-multiple-text-csv-xml-files.html

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2014-04-10
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多