Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Excel VBA)如果单元格值等于&引用;然后显示/隐藏图像_Vba_Excel - Fatal编程技术网

(Excel VBA)如果单元格值等于&引用;然后显示/隐藏图像

(Excel VBA)如果单元格值等于&引用;然后显示/隐藏图像,vba,excel,Vba,Excel,我正在处理一个Excel电子表格,当选择一个下拉框值时,会弹出一个图像,如果选择另一个值,则会隐藏当前图像并弹出与选择相关的图像。我发现了一些方法,仅仅使用纸张和使用坐标定位图像太耗时;这并不是我想要走的路线。在使用StackOverflow之前,我做了很多研究,到目前为止似乎没有任何效果。下面是我努力实现的目标。我试图将所有图像保存在电子表格中,这增加了另一个挑战级别,但我相信有一种方法可以做到这一点,因为excel在插入图像时会为图像指定一个数字,例如图片9 Sub Main() If

我正在处理一个Excel电子表格,当选择一个下拉框值时,会弹出一个图像,如果选择另一个值,则会隐藏当前图像并弹出与选择相关的图像。我发现了一些方法,仅仅使用纸张和使用坐标定位图像太耗时;这并不是我想要走的路线。在使用StackOverflow之前,我做了很多研究,到目前为止似乎没有任何效果。下面是我努力实现的目标。我试图将所有图像保存在电子表格中,这增加了另一个挑战级别,但我相信有一种方法可以做到这一点,因为excel在插入图像时会为图像指定一个数字,例如图片9

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

   Picture1 show

   Picture2 hide

   End If
End Sub
非常感谢您的帮助。谢谢

Sub hidePicture(myImage)
    ActiveSheet.Shapes.Range(Array(myImage)).Select
    Selection.ShapeRange.Height = 0
    Selection.ShapeRange.Width = 0
End Sub

Sub showPicture(myImage)
    ActiveSheet.Shapes.Range(Array(myImage)).Select
    Selection.ShapeRange.Height = 200
    Selection.ShapeRange.Width = 300
End Sub

便利提示:记录宏并查看它生成的代码

最好只是将图片“移出屏幕”,尤其是大小不同的图片

Sub Tester()
    ShowPicture "Picture 3"
End Sub

Sub ShowPicture(PicName As String)

    Dim s As Shape
    For Each s In ActiveSheet.Shapes
        With s
        .Top = IIf(.Name = PicName, 100, 100)
        .Left = IIf(.Name = PicName, 100, 1000)
        End With
    Next s

End Sub

与其隐藏/移动/缩小不需要的图片的大小,为什么不干脆删除它呢

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

这里有一个例子

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
子样本()
选择案例范围(“G11”)。值
案例“图片1”:展示图片(“图片1”)
案例“图片2”:展示图片(“图片2”)
案例“图片3”:展示图片(“图片3”)
案例“图片4”:展示图片(“图片4”)
结束选择
端接头
子ShowPicture(picname作为字符串)
“~~>我之所以使用OERN,是因为它简单得多
“~~>而不是循环所有形状,然后删除它们。可能有
“~~~>图表、命令按钮和其他形状。我得写信了
“~~>额外的验证代码,以便不会删除这些形状。
出错时继续下一步
图纸(“图纸1”)。形状(“图片1”)。删除
图纸(“图纸1”)。形状(“图片2”)。删除
图纸(“图纸1”)。形状(“图片3”)。删除
图纸(“图纸1”)。形状(“图片4”)。删除
错误转到0
图纸(“临时”)形状(图片名称)。复制

“下面是一个使用对象的Visible属性的解决方案。 我使用它来显示基于字段中的值的图片。 该字段有一个导致“好”或“坏”的公式。 如果它的价值是“好的”,我想展示一幅画;对于“坏”,应显示另一张图片;它们不应该同时出现。 每当用户刷新透视表时,字段都需要更新其值,因此我将代码放在工作表的该方法中,透视表和图片将显示在该方法中

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'hide both pictures by loopng through all the shapes on the sheet
Dim s As Shape
For Each s In ActiveSheet.Shapes
'hide the shape if it is a picture, leave other shapes on the page visible.
If s.Type = msoPicture Then s.Visible = msoFalse
Next

Dim judgement As String
'The field whose value tells what picture to use is a one-cell named range called "judgement"
judgement = Range("judgement")

'you need to know which picture is which.
If judgement = "Good" Then ActiveSheet.Shapes("Picture 8").Visible = True
If judgement = "Bad" Then ActiveSheet.Shapes("Picture 1").Visible = True

End Sub

我确实这么做了,但是我从来没有想过把它缩小到零。谢谢你的解决方案,现在就开始尝试。运气不好,一个只允许一个图像,不能做多个图像。我希望使用单元格值可以控制显示的图像