Imported text file to begin from the first empty cell

P

p11p00

Hi

I want to import a textfile to excel worksheet. Problem is, that I want the
imported data to begin in the first empty cell in column A. How do I do this?
So far the macro is:

Sub data_import()

With
ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Opticon\Data\Data.txt" _
, Destination:=Range("A1"))
.Name = "Data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Thanks,
Elina
 
R

RyanH

Add this line to the top of your code:

' finds first empty cell in Col A, starting from top of sheet
lngLastRow = ActiveSheet.Cells(1, "A").End(xlDown).Row + 1

Then change your destination line:

Destination:=Range("A" & lngLastRow)
 
P

p11p00

Thank you RyanH, this works, if I have 2 first rows filled for some reason.
Anyway I can use this.

Cheers,
Elina



"RyanH" kirjoitti:
 
R

RyanH

If the first two rows always have something in them use this.

lngLastRow = ActiveSheet.Cells(2, "A").End(xlDown).Row + 1

I hope this helps! If so, then click "YES" below.
 

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

Top