【发布时间】:2016-03-10 20:17:24
【问题描述】:
我正在研究将文本文件导入工作表的宏。我录制了一个宏并稍微调整了代码以满足我的需要。我需要对多个文件执行此操作,因此我将代码复制并粘贴到其他宏中,但我遇到了问题。第一次迭代效果很好,但是当我运行第二次迭代时,它将我在多列中导入的原始数据从 AA 列开始向右移动,并将下一批数据发布到 A 列中,它应该在该列但更靠下的位置与第一批的最后一行数据。谁能建议为什么这不仅仅是在上一次迭代的最后一行下面添加新数据?这是我的代码。 (已编辑) - 我将粘贴到第二次迭代的代码更改为显示最后一行,而不是第一行被硬编码。
Sub Upload2()
row = ThisWorkbook.Sheets("Data Dump").Cells(Rows.Count, 1).End(xlUp).row
filepath = Worksheets("Upload").Cells(5, "H")
Worksheets("Data Dump").Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" + filepath, Destination:=Cells(row, "A"))
'.CommandType = 0
.Name = "bench test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Dim Cn As Variant
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each Cn In ThisWorkbook.Connections
Cn.Delete
Next Cn
For Each Cn In ActiveSheet.QueryTables
Cn.Delete
Next Cn
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
谢谢 最大
【问题讨论】:
-
在发布错误代码后查看修改后的答案...
-
你试过提供的答案了吗?