Forum Discussion
WLLIAN
Dec 27, 2023Brass Contributor
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 p...
NikolinoDE
Dec 27, 2023Gold Contributor
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.
- WLLIANDec 27, 2023Brass Contributor
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
- NikolinoDEDec 28, 2023Gold Contributor
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:
- 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."
- 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.
- WLLIANDec 28, 2023Brass Contributor
NikolinoDEHi. I try, but i must doing a lot of things wrong. kkkk
I share in One drive, link below if helps 🙂
Windows 10 = 64
Microsoft 365
.xlsm