字母f视为VBA中的选项卡

我有一个VBAmacros读取多个文本文件到一个Excel工作簿中的单独的工作表。 一切正常,除了VBA将字母“f”视为制表符。 (所有文件都是制表符分隔的。)也就是说,当它在string中看到字母“f”时,将string中的所有下游文本推送到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 

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

 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 

 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 

结束小组