(Excel VBA)如果单元格值等于“”然后显示/隐藏图像 [英] (Excel VBA) If Cell Value equals "" Then Show/Hide Images

查看:589
本文介绍了(Excel VBA)如果单元格值等于“”然后显示/隐藏图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Excel电子表格,当选择一个下拉框值时,会弹出一个图像,如果选择了另一个值,它将隐藏当前图像并弹出与选择相关的图像。我发现了几种方法,只是使用坐标来简单地绘制图像并定位图像太费时间了;这不完全是我想去的路线。在使用StackOverflow之前,我已经做了很多研究,至今没有任何效果。以下是我正在努力实现的目标。我试图保留电子表格中的所有图像,这增加了另一个级别的挑战,但我相信有一种方法可以这样做,因为在插入 EX时,excel会将图像分配给一个数字。图片9

  Sub Main()
如果Range(G11).Value =anything然后

Picture1 show

Picture2 hide

如果
End Sub

任何帮助非常感谢。谢谢

解决方案

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

逻辑
将所有图像保存在临时表中。当有相关图片被显示时,从临时表中取出并删除以前的图片。



这是一个例子。

  Sub Sample()
选择案例范围(G11)值
案例Picture 1:ShowPicture(Picture 1 )
案例Picture 2:ShowPicture(Picture 2)
案例Picture 3:ShowPicture(Picture 3)
案例Picture 4:ShowPicture(Picture 4 )
结束选择
End Sub

Sub ShowPicture(picname As String)
'~~>我使用OERN的原因是因为它更简单
'~~>比循环所有形状,然后删除它们。可能有
'~~>图表,命令按钮和其他形状。我必须写
'~~>额外的验证码,使得这些形状不被删除。
关于错误简历Next
表格(Sheet1)形状(图片1)删除
表格(Sheet1)形状(图片2)删除
表格(Sheet1)。形状(图3)。删除
表格(Sheet1)。形状(图4)。删除
错误GoTo 0

Sheets(Temp)。形状(picname).Copy

'< ~~替代下面的行。您可以按照您的要求粘贴图像
'< ~~
表格(Sheet1)。范围(G15)。选择

表格(Sheet1)。粘贴
End Sub

临时表快照




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 up the image related to the selection. I have found a few methods that are just too time consuming using just the sheet and positioning of the image using coordinates; that's not exactly the route I would like to go.I have done a quite a bit of research before using StackOverflow, and nothing seemed to work thus far. Below is what I am trying to achieve. I am trying to keep all the images within the spreadsheet which adds another level of challenge, but I believe there is a way to do this because excel assigns the image a number when inserted EX. Picture 9.

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

   Picture1 show

   Picture2 hide

   End If
End Sub

Any Help is greatly appreciated. Thanks

解决方案

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

这篇关于(Excel VBA)如果单元格值等于“”然后显示/隐藏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆