Find the image with the name condition and bring it to the worksheet

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. Please help with a code that enables me to fetch a specific image from the same workbook path and type its name in cell B2. Because I have more than 100 images in jpg and jpeg format I want to search for the image provided the name and bring it to the worksheet
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
So you want to add an image name into B2 then search the same folder for that image and then??? You want to import it to a specific location does it need to be resized at all. If you have over 100 images to find would it be better to have a list of images and them import them via a loop rather than one at a time?
 
Upvote 0
So you want to add an image name into B2 then search the same folder for that image and then??? You want to import it to a specific location does it need to be resized at all. If you have over 100 images to find would it be better to have a list of images and them import them via a loop rather than one at a time?
Thank you for your interest. I will try to explain more. I have a database of employees I search for employee data by formulas and fetch it to another sheet what I am trying to do is fetch the employee image from outside the workbook whose name matches cell b2

Something like this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("a2")) Is Nothing Then
 Call Find_Value
    Patch = ThisWorkbook.Path
  
 
    Me.Image1.Picture = LoadPicture(Patch & "\" & Range("b2") & ("(*.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png), *.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png"))
  
    Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
    Me.Image1.Left = [L6].Left
    Me.Image1.Top = [L6].Top

 End If
End Sub
 
Upvote 0
See if this works for you.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Patch As String, Filefound As Boolean, Strfile As String, Imgfile As String

If Not Intersect(Target, Range("a2")) Is Nothing Then
    Patch = ThisWorkbook.Path
    Filefound = False
    Strfile = Dir(Patch & "\" & Range("B2").Value & ".*")
    Do While Len(Strfile) > 0
        If Len(Strfile) <> 0 Then
            Filefound = True
            Imgfile = Strfile
            Exit Do
            Else
        End If
    Loop
    If Filefound = True Then
        ActiveSheet.Pictures.Insert(Patch & "\" & Imgfile).Select
        Else
        MsgBox ("File not found")
    End If
End If
End Sub
I could also see in your code you placed the extension twice with a bracket in the middle. That may have been causing some issues.
("(*.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png), *.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png"))
 
Upvote 0
Solution
See if this works for you.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Patch As String, Filefound As Boolean, Strfile As String, Imgfile As String

If Not Intersect(Target, Range("a2")) Is Nothing Then
    Patch = ThisWorkbook.Path
    Filefound = False
    Strfile = Dir(Patch & "\" & Range("B2").Value & ".*")
    Do While Len(Strfile) > 0
        If Len(Strfile) <> 0 Then
            Filefound = True
            Imgfile = Strfile
            Exit Do
            Else
        End If
    Loop
    If Filefound = True Then
        ActiveSheet.Pictures.Insert(Patch & "\" & Imgfile).Select
        Else
        MsgBox ("File not found")
    End If
End If
End Sub
I could also see in your code you placed the extension twice with a bracket in the middle. That may have been causing some issues.
("(*.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png), *.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png"))
Thank you. Your code succeeded in implementing what was required, with some simple modifications to try to standardize the size of the images after importing them and placing them inside a frame.
Thank you very much. I appreciate your help
 
Upvote 1

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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