Insert an image in a specific cell using VBA

agatonsaxx

New Member
Joined
May 29, 2014
Messages
34
Hi, i have a question regarding show/hide an image in a specific cell using VBA. I have the following "code" and i just want the image to be visible and shown in Cell C11, and when i click another button i would like it to be hidden. But in this case, it shows the picture in the centre of the excel document instead of in a specific cell.

Here is the code:

Sub Show_Logotyp_SMAL()
ActiveSheet.Shapes("Picture 19").Visible = True
End Sub


Sub Hide_Logotyp_SMAL()
ActiveSheet.Shapes("Picture 19").Visible = False
End Sub

Anyone know how to do this in an easy way?

Thanks a lot,

Best regards
AgatonSaxx
 

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Aaron Blood

Active Member
Joined
Oct 10, 2002
Messages
485
I would assume the picture is already positioned in the cell, so there would be no need to move it. Changing the visible property of the picture alone does not reposition the picture.

Also, I would use a single subroutine to toggle the visible property instead of separate hide/show subs.
Code:
Sub Toggle_Logotyp_SMAL()
    With ActiveSheet.Shapes("Picture 19")
        .Visible = Not .Visible
    End With
End Sub
 

Aaron Blood

Active Member
Joined
Oct 10, 2002
Messages
485
In the event that the picture (due to some other preceding code) is not in the correct position to begin with...

Code:
Sub Toggle_Logotyp_SMAL()
    Dim Rng As Range
    Set Rng = Range("C3:F10")
    
    Dim MyPic As Shape
    Set MyPic = ActiveSheet.Shapes("Picture 19")
    
    With MyPic
        .Visible = Not .Visible
        .Top = Rng.Top
        .Left = Rng.Left
        .LockAspectRatio = False
        .Height = Rng.Height
        .Width = Rng.Width
    End With
    
End Sub

The LockAspectRatio and Height/Width properties are optional in the event that you also want to fit the picture size to a cell or range automatically. Just remove them or comment them out if you don't want to mess with the size of the logo pic.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,511
Messages
5,602,081
Members
414,500
Latest member
kevdragon1

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
Top