字母f在VBA中被视为选项卡

时间:2014-01-10 18:33:28

标签: excel vba

我有一个VBA宏,可以在一个Excel工作簿中将多个文本文件读入单独的工作表中。一切正常,除了VBA将字母“f”视为标签。 (所有文件都以制表符分隔。)也就是说,当它在字符串中看到字母“f”时,将字符串中的所有下游文本推送到Excel中的下一个字段/单元格。以前有人遇到过这个吗?如果是这样,你是如何解决它的?

Sub MultipleTextFilesIntoExcelSheets()
    Dim i As Integer 'a counter to loop through the files in the folder
    Dim fname As String, FullName As String 'fname is the name of the file, and FullName is the name of its path
    Dim ws As Worksheet 'a workbook object for the workbook where the current macro is running

    i = 0 'seed the counter

    'get the name of the first text file
    fname = Dir("C:\dummy_path\*txt")

    'loop through the text files to put them onto separate sheets in the Excel book
    While (Len(fname) > 0)
        'get the full path of the text file
        FullName = "C:\dummy_path\" & fname
        i = i + 1 'get ready for the next iteration

        Set ws = ThisWorkbook.Sheets("Sheet" & i) 'the current sheet

        With ws.QueryTables.Add(Connection:="TEXT;" & FullName, Destination:=ws.Range("A1"))
            .Name = "a" & i
            .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 = True    'we are using a tab-delimited file
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = False
            .TextFileColumnDataTypes = Array(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) 'there are 21 columns of text type in each file
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        fname = Dir
    Wend
End Sub

以下是我正在阅读的示例文本文件:

Header1 Header2  
finance stuff

我能够修复我的代码。这是解决方案:

Sub MultipleTextFilesIntoExcelSheets()
    Dim i As Integer 'a counter to loop through the files in the folder
    Dim fname As String, FullName As String 'fname is the name of the file, and FullName is the name of its path
    Dim ws As Worksheet 'a workbook object for the workbook where the current macro is running

    i = 0 'seed the counter

    'get the name of the first text file
    fname = Dir("C:\dummy_path\*txt")

    'loop through the text files to put them onto separate sheets in the Excel book
    While (Len(fname) > 0)
        'get the full path of the text file
        FullName = "C:\dummy_path\" & fname
        i = i + 1 'get ready for the next iteration

        Set ws = ThisWorkbook.Sheets("Sheet" & i) 'the current sheet

        With ws.QueryTables.Add(Connection:="TEXT;" & FullName, Destination:=ws.Range("A1"))
            .Name = "a" & i
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 65001 'this is critical
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True    'we are using a tab-delimited file
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            ' .TextFileOtherDelimiter = False 'get rid of this
            .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2) 'all files have 21 columns, data type = text (2)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        fname = Dir
    Wend
End Sub

1 个答案:

答案 0 :(得分:0)

Sub MultipleTextFilesIntoExcelSheets()
    Dim i As Integer 'a counter to loop through the files in the folder
    Dim fname As String, FullName As String 'fname is the name of the file, and FullName is the name of its path
    Dim ws As Worksheet 'a workbook object for the workbook where the current macro is running

i = 0 'seed the counter

'get the name of the first text file
fname = Dir("C:\dummy_path\*txt")

'loop through the text files to put them onto separate sheets in the Excel book
While (Len(fname) > 0)
    'get the full path of the text file
    FullName = "C:\dummy_path\" & fname
    i = i + 1 'get ready for the next iteration

    Set ws = ThisWorkbook.Sheets("Sheet" & i) 'the current sheet

    With ws.QueryTables.Add(Connection:="TEXT;" & FullName, Destination:=ws.Range("A1"))
        .Name = "a" & i
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 65001 'change this value according to the encoding of the text file
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True    'we are using a tab-delimited file
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2) 'all files have 21 columns, data type = text (2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    fname = Dir
Wend

End Sub