(Excel VBA) If Cell Value equals “” Then Show/Hide Images

后端 未结 4 664
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 15:31

I am working on a Excel Spreadsheet that when a dropdown box value is selected an image will pop up, and if another value is selected it will hide the current image and pop

4条回答
  •  既然无缘
    2021-01-12 16:20

    Rather than hiding/moving/reducing the size of the unwanted pic, why not simply delete it?

    Logic: Save all your images in a temp sheet. When ever a relevant picture is supposed to be shown, get it from the temp sheet and delete the previous.

    Here is an example.

    Sub Sample()
        Select Case Range("G11").Value
            Case "Picture 1": ShowPicture ("Picture 1")
            Case "Picture 2": ShowPicture ("Picture 2")
            Case "Picture 3": ShowPicture ("Picture 3")
            Case "Picture 4": ShowPicture ("Picture 4")
        End Select
    End Sub
    
    Sub ShowPicture(picname As String)
        '~~> The reason why I am using OERN is because it is much simpler
        '~~> than looping all shapes and then deleting them. There could be
        '~~> charts, command buttons and other shapes. I will have to write
        '~~> extra validation code so that those shapes are not deleted.
        On Error Resume Next
        Sheets("Sheet1").Shapes("Picture 1").Delete
        Sheets("Sheet1").Shapes("Picture 2").Delete
        Sheets("Sheet1").Shapes("Picture 3").Delete
        Sheets("Sheet1").Shapes("Picture 4").Delete
        On Error GoTo 0
    
        Sheets("Temp").Shapes(picname).Copy
    
        '<~~ Alternative to the below line. You may re-position the image 
        '<~~ after you paste as per your requirement
        Sheets("Sheet1").Range("G15").Select 
    
        Sheets("Sheet1").Paste
    End Sub
    

    Snapshot of temp sheet

    enter image description here

提交回复
热议问题