Choose file for Macro to import

RasmusMoerch

New Member
Joined
Mar 26, 2014
Messages
2
Hi

I have recorded a macro that gets external data from a text file and sorts it. However the macro always gets the same file, from the same location.
How do I make the macro ask me for a file to import?

And yes. This is my first macro... So I need all the help I can get.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
here's 1 i use which gives the user the option to select the file rather than picking one up directly

Code:
Sub ClientMap()
Dim noInfo As String
 
MacroFile = ActiveWorkbook.Name
 
Dim strCurDir As String
 
'// Keep Original Dir
strCurDir = CurDir
 
SetCurrentDirectory "SET DIRECTORY HERE”
 
 'open
newfn = Application.GetOpenFilename(fileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Please select your File")’ change excel version to suit
If newfn = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
Workbooks.Open filename:=newfn
End If
 
 
‘INSERT WHAT YOU NEEED TO DO HERE
 
ActiveWorkbook.Close
 
End Sub
 
Last edited:
Upvote 0
Thanks for the reply.

Some of the stuff I need to do is done in the import wizard. How do I put that in your code?
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;H:\Excell\Fra PHAM\Filename.DTA", _
Destination:=Range("$A$8"))
.Name = "Filename"
.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 = 56
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 9, 9, 1, 1, 1, 9, 9, 9, 9, 9, 9)
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = " "
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 
Upvote 0

Forum statistics

Threads
1,223,643
Messages
6,173,520
Members
452,518
Latest member
SoerenB

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