Dynamic Chart with VBA

Pinkoto

New Member
Joined
Oct 7, 2016
Messages
40
Hello im using this VBA code that makes my chart dynamic.

Code:
<code>Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("K2:L2")) Is Nothing Then
    With Me
        .ChartObjects("Chart 1").Activate
        ActiveChart.SetSourceData Source:=.Range(.Cells(1, 1), .Cells(1 + [K2].Value, 1 + [L2].Value))
    End With
End If
End Sub</code>
<code>I type it in VBA in Sheet 1 where the data source is. If the chart is there too it will work perfectly. But i want to put the chart in Sheet2. Can you help me do this?

Also K2 and L2 keep the values that show how much columns and rows should the chart take from the table. I want to keep them too in Sheet1. THe only thing in sheet2 has to be the chart.
</code>
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Pinkoto,

Cut and paste "Chart 1" from Sheet1 to Sheet2, then make the following modification to the code...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("K2:L2")) Is Nothing Then
    With Me
        [COLOR=#ff0000]Sheets("Sheet2")[/COLOR].ChartObjects("Chart 1").Activate
        ActiveChart.SetSourceData Source:=.Range(.Cells(1, 1), .Cells(1 + [K2].Value, 1 + [L2].Value))
    End With
End If
End Sub

Cheers,

tonyyy
 
Upvote 0
Thank you. But i seem to find another problem. The values in cell K and L are formulas (count). When they are simple numbers the chart works perfectly but when they are count formula it seems to give mixed results in the chart. Is it better if the count is put in the VBA code itself? If so how can i make it ?
 
Upvote 0
You can leave your formulas in K and L, and make the changes below...

Code:
Private Sub Worksheet_Calculate()
    With Me
        Sheets("Sheet2").ChartObjects("Chart 1").Activate
        ActiveChart.SetSourceData Source:=.Range(.Cells(1, 1), .Cells(1 + [K2].Value, 1 + [L2].Value))
    End With
End Sub
 
Upvote 0
THANK YOU!!! I have worked on this project for some time now and with your help i think i have finished it :). I only hope i dont see the need for more debugging.
 
Upvote 0
Actually, using square brackets to reference cells is not as explicit as referencing them by sheet. And I would expect an error if you try to activate a chart not on the active sheet. And you don't need to activate a chart or even have it on the active sheet to modify it in VBA.

So here's what I would use:

Code:
Private Sub Worksheet_Calculate()
    With Me
        Sheets("Sheet2").ChartObjects("Chart 1").Chart.SetSourceData Source:=Range(.Cells(1, 1), .Cells(1 + .Range("K2").Value, 1 + .Range("L2").Value))
    End With
End Sub
 
Upvote 0
thank you!

But everytime i add something to any sheet of the file it automatically sends me to the sheet with the chart like its refreshing it. How can i stop that?
 
Upvote 0
See Tony's code, which activates the chart, then does something to the activechart?

Don't activate the chart. Use my version, which references the chart without activating it, and makes the change.
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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