Hide/view chart based on cell value

AlexPi

Board Regular
Joined
Apr 4, 2011
Messages
104
I'm new to VBA and trying to learn a few tricks from this forum. I have some code below that I cannot get to work correctly.

I want to show or hide 3 different charts based on the cell value in G20. I have a data validation list in G20 that has 3 options, Estimate, Goal, or Actual cost. I want the user to be able to choose a name from the list and a different chart to appear.

I think I'm close but I'm probably missing something...Right now it will only show Chart 2.

Thanks for any help!

Code:
Sub getchart()
 
If Range("G20") = "Estimate" Then
          Sheets("Overview").ChartObjects("BudgetOverview").Visible = True
            Sheets("Overview").ChartObjects("Chart 1").Visible = False
            Sheets("Overview").ChartObjects("Chart 2").Visible = False
If Range("G20") = "Goal" Then
            
         Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
            Sheets("Overview").ChartObjects("Chart 1").Visible = True
            Sheets("Overview").ChartObjects("Chart 2").Visible = False
            
            
Else
            
        Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
          Sheets("Overview").ChartObjects("Chart 1").Visible = False
          Sheets("Overview").ChartObjects("Chart 2").Visible = True            
 
            
     End If
     End If
     
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You need either an end if or else if like this

If Range("G20") = "Estimate" Then
Sheets("Overview").ChartObjects("BudgetOverview").Visible = True
Sheets("Overview").ChartObjects("Chart 1").Visible = False
Sheets("Overview").ChartObjects("Chart 2").Visible = False
End IF

If Range("G20") = "Goal" Then

Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
Sheets("Overview").ChartObjects("Chart 1").Visible = True
Sheets("Overview").ChartObjects("Chart 2").Visible = False
End IF

If Range("G20") = "Actual cost" Then
Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
Sheets("Overview").ChartObjects("Chart 1").Visible = False
Sheets("Overview").ChartObjects("Chart 2").Visible = True
End IF
Or like this

Sub getchart()
If Range("G20") = "Estimate" Then
Sheets("Overview").ChartObjects("BudgetOverview").Visible = True
Sheets("Overview").ChartObjects("Chart 1").Visible = False
Sheets("Overview").ChartObjects("Chart 2").Visible = False
ElseIf Range("G20") = "Goal" Then
Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
Sheets("Overview").ChartObjects("Chart 1").Visible = True
Sheets("Overview").ChartObjects("Chart 2").Visible = False
Else
Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
Sheets("Overview").ChartObjects("Chart 1").Visible = False
Sheets("Overview").ChartObjects("Chart 2").Visible = True
End If
End Sub
 
Upvote 0
Perfect! that's exactly what I needed. Now my next question is can I get it to automatically run that code when the cell value changes?

Thanks for your help!
 
Upvote 0
Use the right mouse button on the worksheet name and select view code, then from the drop downs at the top select the General and change that to Worksheet, Make sure the right drop down shows you SelectionChange and then add your code in there, I have done a test and it seems to work OK.
 
Upvote 0
hmmm...still can't get it to work. When I change the drop down menu's the new code I get is


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("G20").Value = "Estimate" Then
          Sheets("Overview").ChartObjects("BudgetOverview").Visible = True
            Sheets("Overview").ChartObjects("Chart 1").Visible = False
            Sheets("Overview").ChartObjects("Chart 2").Visible = False
ElseIf Range("G20").Value = "Goal" Then
            
         Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
            Sheets("Overview").ChartObjects("Chart 1").Visible = True
            Sheets("Overview").ChartObjects("Chart 2").Visible = False
            
Else
            
        Sheets("Overview").ChartObjects("BudgetOverview").Visible = False
          Sheets("Overview").ChartObjects("Chart 1").Visible = False
          Sheets("Overview").ChartObjects("Chart 2").Visible = True
 
            
     End If
End Sub
 
Upvote 0
Pleased to read you have a working solution Alex.;)
 
Upvote 0
Hello,
Whit this VBA you help me a lot, thanks for that.

I have just one problem and I will be realy happy if you help me.
I make few buttons and I show/hide charts by them. But the problem is that if I clik to button nothing happend. Afte I click somewhere at sheet, just after that the chart shown.
I also try to use ApplicationSreen updating but with no success.

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top