Add image from file path and put text into next cell if successful

babybluebentley

New Member
Joined
Mar 24, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,
I already have a VBA that inserts the corresponding image next to a file path. Works fine so far
VBA Code:
Sub AddImageFromPath()
    Dim xRg As Range
    Dim xCell As Range
    Dim xVal As String
    On Error Resume Next
    Set xRg = Application.Range("C2:C10000")
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    For Each xCell In xRg
        xVal = xCell.Value
        If xVal <> "" Then
            ActiveSheet.Shapes.AddPicture xCell.Value, msoFalse, msoTrue, _
            xCell.Offset(0, 1).Left, xCell.Top, xCell.Height, _
            xCell.Height
        End If
    Next
    Application.ScreenUpdating = True
End Sub
-> file(image) paths are in column C, images gets added into cell next to path

My problem is: As I generate the image path via a formula, not every file path actually has an image. My tool should actually check whether or not there is an image for this path or not, so I would like to have a TRUE if there is an image and a FALSE if there is no image. I think i makes sense to add this marker next to the image.
I already tried to create a formula to check it, works, but its way to slow and Excel crashed after two hours. This is the code i tired:
Code:
Function HASpic(Cell As Range) As Boolean

   Dim Caddress      As String
   Dim Pict          As Object
  
   Application.Volatile
   Caddress = Cell.Address
  
   For Each Pict In Application.Caller.Parent.Pictures
      If Pict.TopLeftCell.Address = Caddress Then
         HASpic = True
         Exit Function
      End If
   Next Pict
   HASpic = False
  
End Function

Now I need a code that combines both and tires to mark if an image got added successfully next to a file path. Alternatively, a code that checks if a file exists with the file path in column C

I would be extremely happy if someone could help.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Not sure why you want True or False in a cell, when you can see if there is an image there or not. :unsure:

However you can if an image exists or not when you add them, like
VBA Code:
Sub AddImageFromPath()
    Dim xRg As Range
    Dim xCell As Range
    Dim xVal As String
    On Error Resume Next
    Set xRg = Application.Range("C2:C10000")
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    For Each xCell In xRg
        xVal = xCell.Value
        If xVal <> "" Then
            If Dir(xVal) = "" Then
                xCell.Offset(, 2).Value = False
            Else
                ActiveSheet.Shapes.AddPicture xCell.Value, msoFalse, msoTrue, _
                xCell.Offset(0, 1).Left, xCell.Top, xCell.Height, _
                xCell.Height
            End If
        End If
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Not sure why you want True or False in a cell, when you can see if there is an image there or not. :unsure:

However you can if an image exists or not when you add them, like
VBA Code:
Sub AddImageFromPath()
    Dim xRg As Range
    Dim xCell As Range
    Dim xVal As String
    On Error Resume Next
    Set xRg = Application.Range("C2:C10000")
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    For Each xCell In xRg
        xVal = xCell.Value
        If xVal <> "" Then
            If Dir(xVal) = "" Then
                xCell.Offset(, 2).Value = False
            Else
                ActiveSheet.Shapes.AddPicture xCell.Value, msoFalse, msoTrue, _
                xCell.Offset(0, 1).Left, xCell.Top, xCell.Height, _
                xCell.Height
            End If
        End If
    Next
    Application.ScreenUpdating = True
End Sub
Thank you so much!! Works 100%.
It added and checked 8500 file paths in a matter of seconds, incredible!
I wanted it to be true or false so I can sort out the paths with no image. Way easier this way than doing it one by one.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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