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

眉间皱痕 提交于 2020-01-21 07:36:26

问题


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


回答1:


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




回答2:


Here is a solution using the Visible property of the object. I used this to show a picture based on a value in a field. The field had a formula that resulted in either "good" or "bad". If its value was "good", I wanted to show one picture; for "bad", another picture should show; and they should never show at the same time. The field needed to update its value whenever a user refreshed a pivot table, so I put the code in that method of the worksheet where the pivot table and picture were to appear.

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



回答3:


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

Handy tip: record macro and look at the code it generates!




回答4:


Might be better just to move your pictures "off screen", particularly if they're of different sizes.

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


来源:https://stackoverflow.com/questions/9198097/excel-vba-if-cell-value-equals-then-show-hide-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!