Dynamic path for importing data

SurVie

New Member
Joined
May 27, 2011
Messages
4
Hello,

I've made a macro for importing data into my excel sheet. I'm now trying to make the path dynamic based on cell values from my excel sheet. However I can't seem to get the code right for this. Can anyone help me with this?

Below i've posted the code, the red part i need to make dynamic based on cell value's in my sheet. C:\prog\data\ is always the same, but the part "4220" and "2010" and "filename.asc" are always different, and i would like to base these 3 on 3 different cell values. Any help would be appreciated!

Sub Import()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\prog\data\4220\2010\filename.asc", Destination _
:=Range("$A$1"))
.Name = "422000-filename_4"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 3
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ";"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board. Something like this:
"TEXT;C:\prog\data\" & Range("A1") & "\&Range("A2") & "\" & Range("A3"),
 
Upvote 0
I've tried doing this myself and came up with this:

Sub Macro2()

path1 = "4220"
Year1 = "2010"
Path2 = "422000-filname.Asc"

Filename = "C:\prog\data\" & path1 & "\" & Year1 & "\" & Path2

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



But this doesn't seem to be working for me.
 
Last edited:
Upvote 0
Thanks, it works like a charm now!

I changed your code a little bit because i still got syntax errors but now it works like this:

& Range("A1") & "\" & Range("A2") & "\" & Range("A3")

Thanks for your help!!
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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