Immediately run macro when import text data.

  • Thread starter Thread starter geniusideas
  • Start date Start date
Why don't you combine the two macros. Import the data in the same macro that
you want to run immediately after the data is imported? I did a learn macro
on a data import and this is the code I got.

With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\TEMP\Test.txt", _
Destination:=Range("C17"))
.Name = "Test"
.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(1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(1, 2, 2, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Application.CommandBars("External Data").Visible = False
 
That is the way you would create a query. the OP said refresh the query -
while he wasn't totally clear, if the querytable already exists then.

Activesheet.QueryTables(1).Refresh BackgroundQuery:=False

would be the only command needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top