PDA

View Full Version : VBA - Importing External Data from Txt Files



asalv
03-27-2017, 02:03 PM
Hey there guys,

This code works when I write the full destination of the file where is now written "diretorio" (inside the Connection property). Can anyone help me, I want the user to write at the textbox the full destination, as it won't be the same every time.



Sub MacroTXT()'
Dim diretorio As String


diretorio = TextBox1


With Sheet1.QueryTables.Add(Connection:= _
"TEXT;diretorio", Destination:=Range( _
"$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ";"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

mdmackillop
03-27-2017, 02:24 PM
Probably better with a browser selection

Dim diretorio As String
diretorio = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If diretorio = False Then Exit Sub


With Sheet1.QueryTables.Add(Connection:= _
"TEXT;diretorio", Destination:=Range( _
"$A$1"))

asalv
03-28-2017, 08:00 AM
Hey Mdmackillop, thank you for you reply!

I've tried the code you've suggested and this one too:

Sub MacroTXT()

Dim diretorio As String
Dim intChoice As Integer


Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False


intChoice = Application.FileDialog(msoFileDialogOpen).Show


If intChoice <> 0 Then


diretorio = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

End If


With Sheet1.QueryTables.Add(Connection:= _
"TEXT;diretorio", Destination:=Range( _
"$A$1"))

But it doesn`t work, it appears the message below:

18792

snb
03-28-2017, 08:51 AM
Provided in Textbox1 e.g. "G:\OF\example.txt"

Use

Sub M_snb()
sheets.add ,sheets(sheets.count),,textbox1.Text
End Sub