VBA To control two charts

posfog

Board Regular
Joined
Jun 2, 2009
Messages
171
Hi All,

I have the following code that enlarges and reduces the size of a named chart

Code:
Sub AccidentBodyPartTrend1()

Dim ws As Worksheet
Dim chto As ChartObject
Dim rSmall As Range, rBig As Range
Static b As Boolean
Set chto = Worksheets("Dashboard").ChartObjects("Bodybarall")
Set rSmall = Worksheets("Dashboard").Range("AF7:AJ15")
Set rBig = Worksheets("Dashboard").Range("B5:Q35")

If b Then
chto.BringToFront
chto.Width = rSmall.Width
chto.Height = rSmall.Height
chto.Left = rSmall.Left
chto.Top = rSmall.Top
Else
chto.BringToFront
chto.Width = rBig.Width
chto.Height = rBig.Height
chto.Left = rBig.Left
chto.Top = rBig.Top
End If
b = Not b
End Sub


But I wondered if the coding can be changed so it also does the same to another chart at the same time but with the following sizes etc
Code:
Set chto = Worksheets("Dashboard").ChartObjects("Bodypieall")
Set rSmall = Worksheets("Dashboard").Range("AK7:AN15")
Set rBig = Worksheets("Dashboard").Range("R5:AN35")

So when i click on the chart attached to the macro then both charts open or reduce at the same time.


Kind Regards
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I'd suggest refactoring the code a little to make maintenance easier. For example:

Code:
Sub AccidentBodyPartTrend1()

Dim ws As Worksheet
Dim rPie As Range, rBar as Range
Static b As Boolean
Set ws = Worksheets("Dashboard")
If b Then
   set rBar = ws.Range("AF7:AJ15")
   set rPie = ws.Range("AK7:AN15")
Else
   set rBar = ws.Range("B5:Q35")
   set rPie = ws.Range("R5:AN35")
End If
   FitChartToRange ws.ChartObjects("Bodybarall"), rBar
   FitChartToRange ws.ChartObjects("Bodypieall"), rPie
   b = Not b
End Sub
Sub FitChartToRange(chto as Chartobject, rng as Range)
chto.BringToFront
chto.Width = rng.Width
chto.Height = rng.Height
chto.Left = rng.Left
chto.Top = rng.Top
end sub
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...11-vba-to-conrtol-two-charts.html#post4895325

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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