2

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:

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:

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!

4

1 回答 1

3

您可以传入一个参数来确定每次应该显示哪个图形。然后在您选择图表类型时,您可以调用该函数一次,传入所选图表类型,它会在您的所有工作表中打开该图表并关闭所有其他图表。

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.Name).Visible = False
        Next
        '''Turn on the one chart type we want'''
        ws.ChartObjects(graphType).Visible = True
    Next
End Sub
于 2013-08-28T21:39:14.717 回答