(Excel VBA)如果单元格值等于“”然后显示/隐藏图像 [英] (Excel VBA) If Cell Value equals "" Then Show/Hide Images
问题描述
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屋!