Excel VBA 文本文件导入为空
Excel VBA Text File Import Is Blank
我正在尝试将 800 多个文本文件导入同一工作簿中各自的作品sheet。代码如下:
Public Sub dImport()
nFile = Dir("R:\O21DIR\*.txt")
Do While nFile <> vbNullString
Set ws3 = Sheets.Add(After:=Sheets(Sheets.Count))
Application.CutCopyMode = False
With ws3.QueryTables.Add(Connection:="TEXT;" & nFile, Destination:=Range("$A"))
.Name = nFile
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 9, 9, 2, 9, 2, 9, 9, 9)
.TextFileFixedColumnWidths = Array(21, 16, 10, 13, 17, 3, 14, 7, 5, 12, 5, 6)
.TextFileTrailingMinusNumbers = True
End With
ws3.Name = nFile
For cnt = ActiveWorkbook.Connections.Count To 1 Step -1
ActiveWorkbook.Connections.Item(cnt).Delete
Next
For cnt = ActiveWorkbook.Queries.Count To 1 Step -1
ActiveWorkbook.Queries.Item(cnt).Delete
Next
nFile = Dir
fRefine
Loop
End Sub
我没有收到任何错误,但我在 sheet 上也什么也没收到。作品 sheet 已创建并正确命名。文本文件中确实有数据。数据导入代码是从录制宏中提取的,所以它确实在某一时刻起作用。
我确实删除了 .Refresh BackgroundQuery:=False
,因为我收到错误 1004。
我missing/doing哪里错了?
在 Office 365 32 位上使用 Excel 2016。我已经在具有相同软件设置的 2 个不同系统上进行了尝试。相同的结果。
我不是主题专家,但通过查看 the documentation,我认为你应该在某处添加一个 Refresh
。
从上面的页面粘贴:
Set shFirstQtr = Workbooks(1).Worksheets(1)
Set qtQtrResults = shFirstQtr.QueryTables.Add( _
Connection := "TEXT;C:\My Documents980331.txt", _
Destination := shFirstQtr.Cells(1,1))
With qtQtrResults
.TextFileParsingType = xlFixedWidth
.TextFileFixedColumnWidths := Array(5,4)
.TextFileColumnDataTypes := _
Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
.Refresh
End With
我正在尝试将 800 多个文本文件导入同一工作簿中各自的作品sheet。代码如下:
Public Sub dImport()
nFile = Dir("R:\O21DIR\*.txt")
Do While nFile <> vbNullString
Set ws3 = Sheets.Add(After:=Sheets(Sheets.Count))
Application.CutCopyMode = False
With ws3.QueryTables.Add(Connection:="TEXT;" & nFile, Destination:=Range("$A"))
.Name = nFile
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 9, 9, 2, 9, 2, 9, 9, 9)
.TextFileFixedColumnWidths = Array(21, 16, 10, 13, 17, 3, 14, 7, 5, 12, 5, 6)
.TextFileTrailingMinusNumbers = True
End With
ws3.Name = nFile
For cnt = ActiveWorkbook.Connections.Count To 1 Step -1
ActiveWorkbook.Connections.Item(cnt).Delete
Next
For cnt = ActiveWorkbook.Queries.Count To 1 Step -1
ActiveWorkbook.Queries.Item(cnt).Delete
Next
nFile = Dir
fRefine
Loop
End Sub
我没有收到任何错误,但我在 sheet 上也什么也没收到。作品 sheet 已创建并正确命名。文本文件中确实有数据。数据导入代码是从录制宏中提取的,所以它确实在某一时刻起作用。
我确实删除了 .Refresh BackgroundQuery:=False
,因为我收到错误 1004。
我missing/doing哪里错了?
在 Office 365 32 位上使用 Excel 2016。我已经在具有相同软件设置的 2 个不同系统上进行了尝试。相同的结果。
我不是主题专家,但通过查看 the documentation,我认为你应该在某处添加一个 Refresh
。
从上面的页面粘贴:
Set shFirstQtr = Workbooks(1).Worksheets(1)
Set qtQtrResults = shFirstQtr.QueryTables.Add( _
Connection := "TEXT;C:\My Documents980331.txt", _
Destination := shFirstQtr.Cells(1,1))
With qtQtrResults
.TextFileParsingType = xlFixedWidth
.TextFileFixedColumnWidths := Array(5,4)
.TextFileColumnDataTypes := _
Array(xlTextFormat, xlSkipColumn, xlGeneralFormat)
.Refresh
End With