Forum Discussion

WLLIAN's avatar
WLLIAN
Brass Contributor
Nov 10, 2024

CHANGE IMAGE WHEN I SELECT ANOTHER NAME ON LIST BOX

Hi, there a way to change the image on userform when i select another name on listbox?

I have a shape that change on the worksheet when i select a name on listbox, but on Userform only change when i initialize.

The images are in another tab.

 

Here the code on the project:

Private Sub UserForm_Initialize()
Dim cObj As ChartObject, iPath
Application.ScreenUpdating = False
With ActiveSheet.Shapes("Imagem 1")
  Set cObj = ActiveSheet.ChartObjects.Add(.Left, .Top, .Width, .Height)
  .Copy: cObj.Select: cObj.Chart.Paste
  iPath = Environ("Temp") & "\" & Format(Now, "hhmmss") & ".jpg"
  cObj.Chart.Export iPath
End With
Image1.Picture = LoadPicture(iPath)
cObj.Delete
End Sub

 

Excel 365, Windows, System 64, Notebook

 

  • Alikoc's avatar
    Alikoc
    Iron Contributor

    Hello,

    Yes it is possible. 

    To dynamically change the image on a UserForm when a new name is selected from a ListBox in Excel, you need to use an event that triggers every time a selection is made in the ListBox, rather than initializing the image only once in UserForm_Initialize.

    The corrected version of the code is as attached. I would be happy if you test and give information and mark it as an answer.

     

    Private Sub UserForm_Initialize()
        ' Populate the ListBox with names (or IDs) from the worksheet or directly
        With ListBox1
            .AddItem "Name1"
            .AddItem "Name2"
            ' Add as many items as needed, or load dynamically from a range
        End With
    End Sub

    Private Sub ListBox1_Change()
        Dim selectedName As String
        Dim imagePath As String
        
        ' Get the selected name from the ListBox
        selectedName = ListBox1.Value
        
        ' Determine the path of the image based on the selected name
        ' Assuming you have a naming convention for your images
        imagePath = ThisWorkbook.Path & "\Images\" & selectedName & ".jpg" ' Adjust the path as needed
        
        ' Load the image into the Image control
        If Dir(imagePath) <> "" Then
            Image1.Picture = LoadPicture(imagePath)
        Else
            MsgBox "Image not found for " & selectedName, vbExclamation
        End If
    End Sub

     

    Best Regards,

    Ali Koc

    • WLLIAN's avatar
      WLLIAN
      Brass Contributor

      " imagePath = ThisWorkbook.Path & "\Images\" & selectedName & ".jpg" ' Adjust the path as needed"

      I dont think this is it.

      The images are in the file, in another tab.
      This you are telling to bring the images from a folder, right?

      I want to load the image shape to image object on userform, then when i select another name change the image automatic.

      thanks dude.

       

       

       

       

       

      • Alikoc's avatar
        Alikoc
        Iron Contributor

        Hello,

        Understand...

        Since the images are in another worksheet within the same Excel file, you can use VBA to copy the image from the worksheet into the UserForm’s Image control dynamically based on the selected item in the ListBox.

        There are some points you should pay attention to.

        Ensure Each Image Has a Unique Name: On the worksheet with the images, each image should have a unique name that corresponds to the names in the ListBox.

        Modify the UserForm Code: Use the ListBox1_Change event to load the corresponding image into the Image control on the UserForm.

        Can you try the command like this?

         

        Private Sub UserForm_Initialize()
            ' Populate the ListBox with names (or IDs) from the worksheet or directly
            With ListBox1
                .AddItem "Name1"
                .AddItem "Name2"
                ' Add as many items as needed, or load dynamically from a range
            End With
        End Sub
        
        Private Sub ListBox1_Change()
            Dim selectedName As String
            Dim ws As Worksheet
            Dim imgShape As Shape
            
            ' Set the worksheet where images are stored
            Set ws = ThisWorkbook.Sheets("ImagesSheet") ' Replace "ImagesSheet" with your actual sheet name
            
            ' Get the selected name from the ListBox
            selectedName = ListBox1.Value
            
            ' Try to find the shape with the same name as selected in the ListBox
            On Error Resume Next
            Set imgShape = ws.Shapes(selectedName)
            On Error GoTo 0
            
            ' If the shape exists, load it into the Image control on the UserForm
            If Not imgShape Is Nothing Then
                ' Export the shape as a temporary image
                Dim tempPath As String
                tempPath = Environ("TEMP") & "\" & selectedName & ".jpg"
                imgShape.Copy
                ws.Paste
                ws.Shapes(ws.Shapes.Count).Export tempPath
                ws.Shapes(ws.Shapes.Count).Delete
                
                ' Load the exported image into the Image control
                Image1.Picture = LoadPicture(tempPath)
                
                ' Optionally delete the temporary file if you want
                Kill tempPath
            Else
                MsgBox "Image not found for " & selectedName, vbExclamation
            End If
        End Sub

        Best Regards,

        Ali Koc

Resources