Sub InsertPicturesWithDialog()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim fullPath As String
Dim picNum As String
Dim shp As Shape
Dim targetCell As Range
Dim folderPath As String
Dim successCount As Long
Dim picWidth As Double
Dim picHeight As Double
' Variables for user input
Dim numberColumn As String
Dim pictureColumn As String
Dim widthInput As String
Dim heightInput As String
Dim unitInput As String
Dim numberCol As Long
Dim pictureCol As Long
Dim widthValue As Double
Dim heightValue As Double
' Get folder path
folderPath = InputBox("Enter the folder path where images are stored:" & vbCrLf & vbCrLf & _
"Example: C:\Users\User\Downloads\Images", _
"Image Folder Path", _
"C:\Users\User\Downloads\")
If folderPath = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
' Ensure folder path ends with backslash
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
' Check if folder exists
If Dir(folderPath, vbDirectory) = "" Then
MsgBox "Folder not found! Please check the path.", vbCritical
Exit Sub
End If
' Get column with numbers
numberColumn = InputBox("Enter the column letter that contains the numbers:" & vbCrLf & vbCrLf & _
"Example: B", _
"Number Column", _
"B")
If numberColumn = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
numberCol = Range(numberColumn & "1").Column
' Get column for pictures
pictureColumn = InputBox("Enter the column letter where pictures should be placed:" & vbCrLf & vbCrLf & _
"Example: J", _
"Picture Column", _
"J")
If pictureColumn = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
pictureCol = Range(pictureColumn & "1").Column
' Get unit of measurement
unitInput = InputBox("Select unit of measurement:" & vbCrLf & vbCrLf & _
"Enter:" & vbCrLf & _
" 'in' or 'inch' for inches" & vbCrLf & _
" 'cm' for centimeters" & vbCrLf & _
" 'mm' for millimeters", _
"Unit of Measurement", _
"cm")
If unitInput = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
' Normalize unit input
unitInput = LCase(Trim(unitInput))
If unitInput <> "in" And unitInput <> "inch" And unitInput <> "cm" And unitInput <> "mm" Then
MsgBox "Invalid unit! Please enter 'in', 'cm', or 'mm'.", vbCritical
Exit Sub
End If
' Get picture width
widthInput = InputBox("Enter picture width in " & unitInput & ":" & vbCrLf & vbCrLf & _
"Example: 7.2 (for " & unitInput & ")", _
"Picture Width", _
"7.2")
If widthInput = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
If Not IsNumeric(widthInput) Then
MsgBox "Invalid width! Please enter a number.", vbCritical
Exit Sub
End If
widthValue = CDbl(widthInput)
' Get picture height
heightInput = InputBox("Enter picture height in " & unitInput & ":" & vbCrLf & vbCrLf & _
"Example: 9.6 (for " & unitInput & ")", _
"Picture Height", _
"9.6")
If heightInput = "" Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
If Not IsNumeric(heightInput) Then
MsgBox "Invalid height! Please enter a number.", vbCritical
Exit Sub
End If
heightValue = CDbl(heightInput)
' Convert to points (1 inch = 72 points)
Select Case unitInput
Case "in", "inch"
picWidth = widthValue * 72
picHeight = heightValue * 72
Case "cm"
picWidth = (widthValue / 2.54) * 72 ' cm to inches to points
picHeight = (heightValue / 2.54) * 72
Case "mm"
picWidth = (widthValue / 25.4) * 72 ' mm to inches to points
picHeight = (heightValue / 25.4) * 72
End Select
' Confirm settings
Dim confirmMsg As String
confirmMsg = "Please confirm your settings:" & vbCrLf & vbCrLf & _
"Folder: " & folderPath & vbCrLf & _
"Number Column: " & numberColumn & vbCrLf & _
"Picture Column: " & pictureColumn & vbCrLf & _
"Picture Size: " & widthInput & unitInput & " x " & heightInput & unitInput & vbCrLf & _
" (" & Format(picWidth / 72, "0.00") & """ x " & Format(picHeight / 72, "0.00") & """)" & vbCrLf & vbCrLf & _
"Continue?"
If MsgBox(confirmMsg, vbYesNo + vbQuestion, "Confirm Settings") = vbNo Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
' Start processing
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, numberCol).End(xlUp).Row
successCount = 0
' Loop through each row
For i = 1 To lastRow
picNum = Trim(ws.Cells(i, numberCol).Value)
If IsNumeric(picNum) And picNum <> "" Then
' Try to find the image file
fullPath = ""
If Dir(folderPath & picNum & ".jpeg") <> "" Then
fullPath = folderPath & picNum & ".jpeg"
ElseIf Dir(folderPath & picNum & ".jpg") <> "" Then
fullPath = folderPath & picNum & ".jpg"
ElseIf Dir(folderPath & picNum & ".png") <> "" Then
fullPath = folderPath & picNum & ".png"
End If
If fullPath <> "" Then
Set targetCell = ws.Cells(i, pictureCol)
' Delete existing shapes/pictures in target column for this row
On Error Resume Next
Dim existingShp As Shape
For Each existingShp In ws.Shapes
If existingShp.TopLeftCell.Row = i And _
existingShp.TopLeftCell.Column = pictureCol Then
existingShp.Delete
End If
Next existingShp
On Error GoTo 0
' Add picture as EMBEDDED
Set shp = ws.Shapes.AddPicture( _
Filename:=fullPath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=targetCell.Left + 5, _
Top:=targetCell.Top + 5, _
Width:=picWidth, _
Height:=picHeight)
' Set placement to move/size with cells
With shp
.LockAspectRatio = msoFalse
.Placement = xlMoveAndSize
End With
successCount = successCount + 1
End If
End If
Next i
MsgBox successCount & " picture(s) embedded successfully!", vbInformation
End Sub