dynamic file path in VBA

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have this file path to access my pictures folder. I am trying to do something a bit weird here: if the first item in a combobox named cmb1 is selected, I wanna use the folder "Picture1", if the second item is selected, then I wanna select the folder "Picture2 " etc.
Code:
fPath = ThisWorkbook.Path & "\" & "Picture1"

How do I get this done from the line above or anything cool?
Regards
Kelly
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Perhaps something like this:-

Code:
[COLOR="Navy"]Sub[/COLOR] MG20Sep18
[COLOR="Navy"]Dim[/COLOR] Fld [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] fPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] ComboBox1.Value
    [COLOR="Navy"]Case[/COLOR] "pic1", "pic2", "pic3", "pic4", "pic5": Fld = "Picture1"
    [COLOR="Navy"]Case[/COLOR] "pic6", "pic7", "Pic8", "pic9", "pic10": Fld = "Picture2"
    [COLOR="Navy"]Case[/COLOR] "pic11", "pic12", "Pic13", "pic14", "pic15": Fld = "Picture3"
[COLOR="Navy"]End[/COLOR] Select
fPath = ThisWorkbook.Path & "\" & Fld
MsgBox fPath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Kelly,

If your combobox is a userform control or an activeX control then

Code:
fPath = ThisWorkbook.Path & "\" & "Picture" & cmb1.listindex + 1
 
Upvote 0
Perhaps something like this:-

Code:
[COLOR="Navy"]Sub[/COLOR] MG20Sep18
[COLOR="Navy"]Dim[/COLOR] Fld [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] fPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] ComboBox1.Value
    [COLOR="Navy"]Case[/COLOR] "pic1", "pic2", "pic3", "pic4", "pic5": Fld = "Picture1"
    [COLOR="Navy"]Case[/COLOR] "pic6", "pic7", "Pic8", "pic9", "pic10": Fld = "Picture2"
    [COLOR="Navy"]Case[/COLOR] "pic11", "pic12", "Pic13", "pic14", "pic15": Fld = "Picture3"
[COLOR="Navy"]End[/COLOR] Select
fPath = ThisWorkbook.Path & "\" & Fld
MsgBox fPath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

Hello MickG,
I will test this when I get home. Buy the Pic1 etc, what are they?
Thanks.
 
Upvote 0
Hi Kelly,

If your combobox is a userform control or an activeX control then

Code:
fPath = ThisWorkbook.Path & "\" & "Picture" & cmb1.listindex + 1

Hello Zubair,
I think I understand your code and will do what I wanna achieve. Will get you posted when I get home and test it. Thanks
Kelly

PS.
If there is an error that's where cmb1 is blank, which error handler should I use? Maybe
Code:
Exit Sub
 
Last edited:
Upvote 0
Your thread specifies :- "I wanna use the folder "Picture1", if the second item is selected, then I wanna select the folder "Picture2 " etc. " ", I assumed that these are the folder Names and not the name of the pictures within some Folder.

Assuming that is the case, the actual Picture files would be names in your combobox i.e. "Pic1,Pic2,Pic3 etc and held in the various Folders "Picture1, "Picture2" etc.
Then the Complete Path should be something like below:-
Code:
fPath = ThisWorkbook.Path & "\" & Fld & "\" & ComboBox1.Value & ".jpg"
 
Upvote 0
Hi Kelly,

If your combobox is a userform control or an activeX control then

Code:
fPath = ThisWorkbook.Path & "\" & "Picture" & cmb1.listindex + 1

Okay. I have tested it and it has worked just fine. I don't think I even need that error handler. I already have others taking care of the errors that may arise. Thanks again
Kelly
 
Upvote 0
Your thread specifies :- "I wanna use the folder "Picture1", if the second item is selected, then I wanna select the folder "Picture2 " etc. " ", I assumed that these are the folder Names and not the name of the pictures within some Folder.

Assuming that is the case, the actual Picture files would be names in your combobox i.e. "Pic1,Pic2,Pic3 etc and held in the various Folders "Picture1, "Picture2" etc.
Then the Complete Path should be something like below:-
Code:
fPath = ThisWorkbook.Path & "\" & Fld & "\" & ComboBox1.Value & ".jpg"

Hello MickG,
The combobox does not house a name of the pictures. It has kofi relation. The only logic behind my request which I termed "weird" is to reference a particular folder based on the first item 8n the combobox. I think my first explanation was not that clear. Haha. Sometimes I find it very tough to communicate the exact thing on my mind. Still working on it though.

Zubair's code solved it for me.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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