VBA : Change Path or Location A Folder With Automatically

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,102
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all...

i have vba code to insert picture into a cell...this code work properly but i want this code can "looking for" a folder that contains images..
Before it, i must change that path/location every time to set locate folder if i change my photos..
here this code :

Code:
Sub ReplaceImages()    
If Range("AM8") = "" Then Exit Sub
    Dim MyPic As String
    For i = 1 To 4
        MyPic = [COLOR=#ff0000]"d:\myfotos[/COLOR]\" & Range("AM8").Value & ".jpg"
        With Me.Shapes("Pic_" & i).Fill
            .Visible = msoTrue
            On Error Resume Next
            .UserPicture MyPic
        End With
    Next i
End Sub
Private Sub CommandButton1_Click()
    ReplaceImages
End Sub

how to make that code not change manually typing path/location ..i want the code work like can "browse" a folder that i want it..

for everyone would help me, greatly appreciated...

.sst
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Selamat Petang,

If you just want the option of selecting the folder before running your code, try this:

Code:
Dim folderspec As String    ' Open the Browse Folder dialog
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .InitialFileName = "C:\"
            If .Show = 0 Then   'User pressed Cancel
            Exit Sub
            End If
            folderspec = .SelectedItems(1)
        End With

Then use:

Code:
[COLOR=#333333]MyPic = folderspec[/COLOR][COLOR=#333333] & Range("AM8").Value & ".jpg"[/COLOR]

Regards,

CJ
 
Upvote 0
hi Mr...
after combine your code then like this :
Sub ReplaceImages()
If Range("AM8") = "" Then Exit Sub
Dim MyPic As String
For i = 1 To 4
Dim folderspec As String ' Open the Browse Folder dialog
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = "C:"
If .Show = 0 Then 'User pressed Cancel
Exit Sub
End If
folderspec = .SelectedItems(1)
End With
MyPic = folderspec & Range("AM8").Value & ".jpg"
With Me.Shapes("Pic_" & i).Fill
.Visible = msoTrue
On Error Resume Next
.UserPicture MyPic
End With
Next i
End Sub

but i can't see how the code it's work to insert picture in cell...how to finish.?

just like this after running above code :

https://www.screencast.com/t/4wW5ygpUhGYy
 
Last edited:
Upvote 0
I'm sorry, I don't understand your question. In your first post you said that your code was working properly and just wanted a way to browse to a certain folder, which is the code I supplied. Please explain further what it is you are looking for.

Regards,

CJ
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...electing-the-folder-before-running-stuck.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,224,879
Messages
6,181,530
Members
453,054
Latest member
ezzat

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