0% found this document useful (0 votes)
6 views2 pages

Image Into Excel

The provided VBA script inserts images into an Excel worksheet based on descriptions found in a specified column. It searches for images in subfolders of a designated parent folder and embeds them in the 'Picture' column of the sheet. Error handling is included to ensure the parent folder and required sheet exist, and to confirm the presence of the 'Picture' column.

Uploaded by

xecat15162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Image Into Excel

The provided VBA script inserts images into an Excel worksheet based on descriptions found in a specified column. It searches for images in subfolders of a designated parent folder and embeds them in the 'Picture' column of the sheet. Error handling is included to ensure the parent folder and required sheet exist, and to confirm the presence of the 'Picture' column.

Uploaded by

xecat15162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub InsertPictures()

Dim parentFolder As String


Dim subfolder As Object
Dim fileName As String
Dim imgPath As String
Dim desc As String
Dim ws As Worksheet
Dim lastRow As Long
Dim img As Shape
Dim cell As Range
Dim fso As Object
Dim pictureColumn As Long

' Set the parent folder path where your subfolders are located
parentFolder = "D:\Office\Pictures Footwears AlNasser\" ' Update this path

' Check if the parent folder exists


If Len(Dir(parentFolder, vbDirectory)) = 0 Then
MsgBox "Parent folder does not exist: " & parentFolder, vbExclamation
Exit Sub
End If

' Set the active sheet


On Error Resume Next ' Temporarily ignore errors to handle invalid sheet name
Set ws = ThisWorkbook.Sheets("Sheet") ' Change to your sheet name if needed
On Error GoTo 0 ' Turn off the error ignoring

' Check if the sheet exists


If ws Is Nothing Then
MsgBox """FM"" sheet not found.", vbExclamation
Exit Sub
End If

' Find the column number for the "Picture" column by searching for the header
pictureColumn = 0
For Each cell In ws.Rows(1).Cells ' Search in the first row
If cell.Value = "Picture" Then
pictureColumn = cell.Column
Exit For
End If
Next cell

' If the "Picture" column was found


If pictureColumn = 0 Then
MsgBox """Picture"" column not found.", vbExclamation
Exit Sub
End If

' Get the last row of data in column A (Desc1)


lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Create a FileSystemObject to work with files and folders


Set fso = CreateObject("Scripting.FileSystemObject")

' Loop through each row in Desc1 column


For Each cell In ws.Range("A2:A" & lastRow) ' Assuming data starts from row 2
desc = cell.Value

' Initialize the imgPath variable to an empty string for each iteration
imgPath = ""

' Search for image in subfolders


For Each subfolder In fso.GetFolder(parentFolder).Subfolders
' Check for image files with matching name
If fso.FileExists(subfolder.Path & "\" & desc & ".jpg") Then
imgPath = subfolder.Path & "\" & desc & ".jpg" ' Change to .png
or .jpeg if needed
Exit For
End If
Next subfolder

' If a matching image is found, insert and embed the image into the
"Picture" column
If imgPath <> "" Then
' Insert the image as an embedded object using Shapes.AddPicture
Set img = ws.Shapes.AddPicture( _
Filename:=imgPath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoCTrue, _
Left:=cell.Offset(0, pictureColumn - 1).Left, _
Top:=cell.Offset(0, pictureColumn - 1).Top, _
Width:=-1, Height:=-1)

' Adjust image size if necessary


img.LockAspectRatio = msoFalse ' Allow resizing freely
img.Width = cell.Offset(0, pictureColumn - 1).Width
img.Height = cell.RowHeight ' Match the height of the row
End If
Next cell
End Sub

You might also like