VBA Macro Input Box to pull Jpg Image from a folder

maurovescera

New Member
Joined
Mar 3, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am hoping someone can help me, I am trying to write a macro in VBA that a user can use an input box to select their folder an image will be located, and the title of the image will be in Column A and the image will be loaded to column based on the name in column A. My code is below and it pulls the image now, but needs the folder location manually input in the macro, and cannot get the input box to work without selecting the title name. Any suggestions?

Sub InsertPicsr1()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Dim shpPic As Shape

Application.ScreenUpdating = False
fPath = "Q:\Mauro Vescera\Sourcing Projects\Article_Image_Test\"
Set rng = Range("A1:A100000" & Cells(Rows.Count, 4).End(xlUp).Row)
For Each r In rng
On Error GoTo errHandler
If r.Value <> "" Then
Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-20, Height:=-20)
With shpPic
.LockAspectRatio = msoTrue
If .Width > Columns(2).Width Then .Width = Columns(2).Width
Rows(r.Row).RowHeight = .Height
End With
End If
errHandler:
If Err.Number <> 0 Then
Debug.Print Err.Number & ", " & Err.Description & ", " & r.Value
On Error GoTo -1
End If
Next r
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You can use this line to allow a user to browse to a folder and select one or more JPG files.

VBA Code:
Dim vFiles As Variant
vFiles = Application.GetOpenFilename(Title:="Select JPG file(s)", FileFilter:="JPG Files (*.jpg),*.jpg", MultiSelect:=True)

This does not open the files, it only returns the file names selected in the dialog.

If MultiSelect is False, GetOpenFileName returns a string the full path/filename of one file.
If MultiSelect is True, GetOpenFileName returns an array with the full path/file name of each selected file.

If the user cancels the dialog, GetOpenFileName returns the Boolean value False. To test for this, use this:
Code:
If TypeName(vFiles) = "Boolean" then ' exit the sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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