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

后端 未结 4 628
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 15:31

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

相关标签:
4条回答
  • 2021-01-12 16:19

    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
    
    0 讨论(0)
  • 2021-01-12 16:20

    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

    enter image description here

    0 讨论(0)
  • 2021-01-12 16:20
    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!

    0 讨论(0)
  • 2021-01-12 16:27

    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
    
    0 讨论(0)
提交回复
热议问题