Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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

like image 806
Russell Saari Avatar asked Feb 08 '12 17:02

Russell Saari


People also ask

How do you display a picture in Excel based on a cell reference?

Using a named range as the source for a linked picture Click Home -> Copy (or Ctrl + C) to copy the cell. Select a different cell (use E2 if working with the example file). Click Home -> Paste (drop-down) -> Linked Picture (alternatively, the Camera Tool is an option). The pasted image will appear.

How do I hide rows in Excel based on cell value macro?

To do this, first select the data that you want to filter. Then, click the Data tab on the ribbon and click the Filter button. In the drop-down menu that appears, click the column that you want to filter by and then uncheck the box next to the value that you want to hide. Finally, click OK.


2 Answers

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

like image 143
Siddharth Rout Avatar answered Nov 07 '22 00:11

Siddharth Rout


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
like image 42
Rhys Avatar answered Nov 07 '22 01:11

Rhys