Hello to all of you,
I created a VBA script to import a text file in a specif spreadsheet columns range (AK5:AR44) and I would like to make it more efficient with following modifications for which I may need your help:
Use as Destination:=Range the first empty column in the predefined range
Create a sort of loop, after each file is imported, using a pop up window, user should be able to decide whether to import another file to the first new empty column or to stop the import.
Below the code I have create until now, unfortunalety I wasn't able to add a sort of dynamic destination for the newly imported data.
Thanks in advance for your help.
Sub Import_samples_data_rep()
Dim Sample1_repfile As String
MsgBox "Select text file", vbOKOnly
Sample1_repfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
Dim Stuff
'assign variables
On Error GoTo ErrHandler:
ErrHandler: If Err.Number = 1004 Then
ErrMsg = Error(Err.Number)
Exit Sub
End If
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Sample1_repfile, Destination:=Range("$AK$5"))
.Name = "*.txt"
.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 = 14
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 9, 1, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Dim answer As Integer
answer = MsgBox("Import another file?", vbQuestion + vbYesNo + vbDefaultButton2, "Import again")
If answer = vbYes Then
Sample2_repfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Sample2_repfile, Destination:=Range("$AL$5"))
.Name = "*.txt"
.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 = 14
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 9, 1, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Else
MsgBox "File import ended", vbOKOnly
End If
End Sub
Bookmarks