Setting charts on multiple sheets invisible vba

kywe665

New Member
Joined
Jul 19, 2013
Messages
6
I have about 9 different sheets, but they all will have 4 types of graphs (totals, comparison, bydate, trend). I am writing vba to conditionally show one type of graph on every sheet. For example, if I want to show the totals graphs, I want all of my sheets in the workbook to update. I can already make the graphs go invisible and visible on one sheet, but I would like to do so on all sheets. Here's my code now:
Code:
Sub UpdateGraph()
 Sheets(".graphManager").ChartObjects("Totals").Visible = False
End Sub
I want to be able to do this on all sheets so I tried something like this:
Code:
Sub UpdateGraph()
 Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Visible Then ws.Select (False)
ActiveWorksheet.ChartObjects("Totals").Visible = False
    Next
End Sub
But no luck. I do not want to manually type all the sheet names into an array because I may add more sheets in the future and don't want to keep changing the code. How can I loop through all sheets and set a graph named "Totals" to invisible? Or can I just set all graphs in the workbook named "Totals" to invisible without looping through the sheets? Thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try something like this..

Code:
[color=darkblue]Sub[/color] UpdateGraph()
    [color=darkblue]Dim[/color] ws [color=darkblue]As[/color] Worksheet
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
    [color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] Worksheets
        ws.ChartObjects("Totals").Visible = [color=darkblue]False[/color]
    [color=darkblue]Next[/color]
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

If the sheet doesn't have a "Totals" chart, it ignores the error.
 
Upvote 0
Try something like this..
Code:

Sub UpdateGraph() Dim ws As Worksheet On Error Resume Next For Each ws In Worksheets ws.ChartObjects("Totals").Visible = False Next On Error GoTo 0End Sub
If the sheet doesn't have a "Totals" chart, it ignores the error.
Excellent! On another forum someone also suggested this modular function:
Code:
Sub UpdateGraph(graphType As String)
 Dim ws As Worksheet
    For Each ws In Sheets        For Each co In ws.ChartObjects
            '''Turn off all charts on the sheet first'''
            ws.ChartObjects(co).Visible = False
        Next
        '''Turn on the one chart type we want'''
        ws.ChartObjects(graphType).Visible = True
    Next
End Sub
 
Upvote 0
You're welcome.

Please read this about cross posting.

Posting Rules #24
Cross-posting is the term we use when a person has posted the same question to multiple online forums. While there is nothing actually "wrong" with this, it is asked that you at least mention that the question has been posted elsewhere, including a link to the thread on the other forum. There are many people who post at several different forums; threads are cross-posted without stating it are easily identified and pointed out as such.

Link: A message to forum cross posters
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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