在 Excel 2010 中导入文本 - 覆盖以前的导入

Noo*_*oob 4 import vba macros microsoft-excel

我创建了一个宏,使用记录器,在我的 Excel 文件中添加一个特定的 .txt 文件,使用特定的快捷键,在打开的 Excel 文件的特定区域。但是,我遇到了这个问题:当我再次使用宏时,新数据不会覆盖同一区域中导入的先前数据,而是根据需要移动以保持先前数据不变。

有没有办法改变这一点,无论是通过一个选项还是通过改变宏代码?具体来说,我希望宏检查要写入的单元格中是否已经有数据,并在导入文件数据之前清除它们的内容。

到目前为止的代码(重新编码的宏的结果:)

Sub testimport()
'
' testimport Macro
'

'
With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\egw\Desktop\ÍÝïò öÜêåëïò\Book2.txt", Destination:=Range( _
    "M14"))
    .Name = "Book2"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 737
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
End Sub
Run Code Online (Sandbox Code Playgroud)

Cha*_*eRB 7

更改.RefreshStyle = xlInsertDeleteCells.RefreshStyle = xlOverwriteCells,它将用文件中的数据替换单元格。这会强制刷新以将表中的数据替换为与文件中的文本保持同步。