(Excel VBA)如果单元格值等于""则显示/隐藏图像

Rus*_*ari 5 excel vba excel-vba

我正在研究Excel电子表格,当选择下拉框值时,将弹出一个图像,如果选择了另一个值,它将隐藏当前图像并弹出与选择相关的图像.我找到了一些方法,只使用工作表和使用坐标定位图像太费时间; 这不是我想要的路线.在使用StackOverflow之前我做了很多研究,到目前为止似乎没有任何工作.以下是我想要实现的目标.我试图将所有图像保留在电子表格中,这增加了另一层次的挑战,但我相信有一种方法可以做到这一点,因为excel在插入EX时会为图像分配一个数字.图9.

Sub Main()
   If Range(G11).Value = "anything" Then

   Picture1 show

   Picture2 hide

   End If
End Sub
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.谢谢

Sid*_*out 5

而不是隐藏/移动/减少不需要的图片的大小,为什么不简单地删除它?

逻辑:将所有图像保存在临时表中.当应该显示相关图片时,从临时表中获取并删除之前的图片.

这是一个例子.

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
Run Code Online (Sandbox Code Playgroud)

临时表的快照

在此输入图像描述