Forum Discussion

WLLIAN's avatar
WLLIAN
Brass Contributor
Dec 27, 2023

EXCEL - IMAGE ACTIVE CELL

Hi, there a way to pull images from another worksheet?

I use this code, but only active by a folder.

He create a shape Image beside the active cell.

Lets say you have to send to someone, but the person will not see because he do not have the folder with the images. Please help

 

***

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'FOTO AO SELECIONAR CELULA'

On Error Resume Next

If Target.Row > 1 And Target.Value <> "" Then
Me.Shapes.Range(Array("FOTO")).Delete

Dim FullImagePath As String

FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".jpg"
If Dir(FullImagePath) = "" Then
FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".jpeg"
If Dir(FullImagePath) = "" Then
FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".png"
If Dir(FullImagePath) = "" Then Exit Sub
End If
End If

Me.Pictures.Insert(FullImagePath).Select
With Selection
.Name = "foto"
.Left = 400
.Height = 100
.ShapeRange.Shadow.Type = msoShadow21 'Adciona sombra
End With

Target.Activate
Else
Me.Shapes.Range(Array("FOTO")).Delete
End If

End Sub

 

 

EXEMPLE WHAT I WANT TO DO:

    • WLLIAN's avatar
      WLLIAN
      Brass Contributor

      The images it will be in another worksheet.
      Sorry your file is japonese. I dont understand kkk

  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    WLLIAN 

    If you're in a fully updated Excel 365, there's no need to use code because the version supports 'place in cell' for pictures.

     

    It's easy to set up. Even if you have a folder with multiple images.  When you insert the pictures Excel will place them vertically in the column, 1 picture in 1 cell as far as needed.

     

    You could start by setting up a sheet with the pictures like this:

     

    Next, you setup a sheet with the input cells as such (Your arrangement may be different, but you get the idea):

     

    This gif shows what it looks like in motion:

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    WLLIAN 

    If you want to embed the images directly into the Excel workbook rather than relying on external image files, you can maybe use the following modified VBA code. This code embeds the images into the worksheet and displays them in a shape (image) next to the active cell:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        'FOTO AO SELECIONAR CELULA'
    
        On Error Resume Next
    
        ' Delete the existing image shape
        Me.Shapes.Range(Array("FOTO")).Delete
    
        If Target.Row > 1 And Target.Value <> "" Then
            Dim FullImagePath As String
            Dim imgShape As Shape
    
            ' Get the full path to the image file
            FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".jpg"
            If Dir(FullImagePath) = "" Then
                FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".jpeg"
                If Dir(FullImagePath) = "" Then
                    FullImagePath = ThisWorkbook.Path & "\FOTO\" & Target.Value & ".png"
                    If Dir(FullImagePath) = "" Then Exit Sub
                End If
            End If
    
            ' Insert the image into the worksheet
            Set imgShape = Me.Shapes.AddPicture(FullImagePath, _
                              MsoTriState.msoFalse, MsoTriState.msoCTrue, _
                              Target.Left + Target.Width, Target.Top, 100, 100)
    
            ' Customize the image shape
            With imgShape
                .Name = "FOTO"
                .Shadow.Type = msoShadow21 ' Add shadow
            End With
    
            Target.Activate
        End If
    End Sub

    This code uses the Shapes.AddPicture method to insert the image directly into the worksheet. The image is placed next to the active cell, and you can customize the size and position according to your requirements.

    Remember that embedding images directly into the workbook can increase its file size. If file size is a concern, consider keeping the images in a separate folder or using a different solution based on your specific needs. The text was created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

    • WLLIAN's avatar
      WLLIAN
      Brass Contributor

      NikolinoDE 

       

      Failed. kkk

      It work with the folder the code you send to me :), but is not the way i wish.

      Thank you for the advice of the file size, but is just some images.

      I have that project on my pc, its beautiful.

      But i wish to send to someone and that person open the file and see the file just like me.

      I made a lot of research and i could not find anything like that.

      Most videos on youtube or sites only teach with Folders, never on the Worksheet.

      I could you vlookup, but is not dynamic.

      I could use function Image, but is not dynamic.

      I Could use an Object, but is not dynamic.

       

      After you see the video, you will agree with me

       

       

      https://www.youtube.com/watch?v=7OJI2uTc2II&t=71s&pp=ygUWZXhjZWwgaW1hZ2VtIGFvIGNsaWNhcg%3D%3D

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        WLLIAN 

        It seems you are looking for a solution to dynamically display images in a shape based on the active cell's value, and you want the images to be stored in another worksheet. Unfortunately, Excel does not have a built-in feature to dynamically display images (so far I know) in this way without relying on external files or folders.

        However, you can create a workaround using Excel's Picture function and named ranges. Here's a general approach:

        1. Create a Named Range for Each Image:
          • In the worksheet where the images are stored, create a named range for each image. You can do this by selecting the cell with the image, going to the "Formulas" tab, and choosing "Define Name."
        2. Modify the VBA Code:
          • Adjust the VBA code to use the Picture function and reference the named ranges dynamically. Replace the existing VBA code with the following:

        Vba code (is untested):

        Private Sub Worksheet_SelectionChange(ByVal Target As Range)
            On Error Resume Next
        
            ' Delete the existing image shape
            Me.Shapes.Range(Array("FOTO")).Delete
        
            If Target.Row > 1 And Target.Value <> "" Then
                Dim imgName As String
                Dim imgShape As Shape
        
                ' Get the name of the image range based on the target value
                imgName = "Image_" & Target.Value
        
                ' Insert the image into the worksheet
                Set imgShape = Me.Pictures.Insert(ThisWorkbook.Names(imgName).RefersToRange.ImageBytes)
        
                ' Customize the image shape
                With imgShape
                    .Name = "FOTO"
                    .Left = Target.Left + Target.Width
                    .Top = Target.Top
                    .ShapeRange.LockAspectRatio = MsoTriState.msoCTrue
                    .ShapeRange.Width = 100
                    .ShapeRange.Height = 100
                    .Shadow.Type = msoShadow21 ' Add shadow
                End With
            End If
        End Sub

         

        3. Create Named Ranges for Images:

          • Ensure that each image in the image worksheet has a unique name (e.g., Image_Apple, Image_Orange).

        With this approach, each image is treated as a named range, and the VBA code dynamically inserts the image based on the active cell's value.

        Please note that this is a simplified solution, and you may need to adapt it based on your specific requirements and the structure of your workbook. Additionally, ensure that the image names in the image worksheet match the expected names in the VBA code.

         

        If this doesn't help you, please provide more detailed information. Excel version, operating system (Mac, Win., Server, etc.), storage medium (Sharepoint, OneDrive, etc.), file extension (xlsx, xlsm, etc.). They wrote this, The images it will be in another worksheet. Do you mean worksheet or workbook? More detailed information will help to suggest a more precise solution.

Resources