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

后端 未结 4 630
佛祖请我去吃肉
佛祖请我去吃肉 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: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
    

提交回复
热议问题