4

I created a macro, using the recorder, to add a specific .txt file in my Excel file, using a specific shortcut key, in a specific area of the open excel file. However, I've got this problem: when I use the Macro again, the new data does not override the previous data imported, in the same area, but shifts as much as needed to leave the previous data untouched.

Is there any way to change this, either with an option or through altering the macro code? Specifically, I would like the macro to check if the cells to be written already have data in them, and clear their contents before importing the file data.

Code so far (result of a recoded macro:)

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
6
  • Can I assume you can't just clear the worksheet first?
    – Dave
    Oct 23, 2013 at 10:19
  • Would clearing the sheet or range be a suitable solution before import? Can you post the code you are already using?
    – CharlieRB
    Oct 23, 2013 at 11:16
  • To Dave: I want to import the txt file periodically through the macro, and the data of the txt file will be constantly be updated, thus I want the clearance of the already imported - and so out of date - data to be part of this automatic process. To Charlie: Edit on the way. The code is not the final one, but a sample to help me determine what to do, and it is the result of a recorded macro. Can you please remind me what I use to put code in the question? Thank you in advance.
    – Noob Doob
    Oct 23, 2013 at 11:24
  • I didn't get your To Dave - If you want to alert some one, put a @ first. EG, @DaveRook would have alerted me!
    – Dave
    Oct 23, 2013 at 12:41
  • @DaveRook I am not that familiar. Thanks for pointing it out!
    – Noob Doob
    Oct 23, 2013 at 13:14

1 Answer 1

7

Change the .RefreshStyle = xlInsertDeleteCells to .RefreshStyle = xlOverwriteCells and it will replace the cells with the data from the file. This forces the refresh to replace the data in your table to be up to date with the text in the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .