Selecting file to import with macro

bkeat

New Member
Joined
Jan 2, 2013
Messages
22
Hi

I am new to using Excel and vba and would very much appreciate any help on my issue. I am importing two .txt files into my excel workbook in two sheets with a macro. Once imported i will edit the data with a macro. When i run the macro it open a window to select the file but when i click ok i am getting an error and i think its with this line of code

With ActiveSheet.QueryTables.Add(Connection:=selectFile, _
Destination:=Range("$A$1"))




Dim selectFile As String


selectFile = Application.GetOpenFilename(FileFilter:="Text files, *.txt")
If sFile = "False" Then Exit Sub


Sheets("OPA Forecast").Select
Range("A1").Select


With ActiveSheet.QueryTables.Add(Connection:=selectFile, _
Destination:=Range("$A$1"))
.Name = "OPA_Fertility_Forecast_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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With



Thanks in advance with any expertise you may be able to provide.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome to MrExcel.

Try:

Rich (BB code):
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & selectFile, _
Destination:=Range("$A$1"))

By the way sFile should be selectFile here:

Rich (BB code):
If sFile = "False" Then Exit Sub
 
Upvote 0
Thanks, that was a rapid response. It is working now much appreciated and Happy New Year :)
 
Upvote 0
Also could this code be used for importing an another Excel file to keep the code consistent?
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top