How to link macro to chart when sheet is copied?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hi All,

I have a protected worksheet that includes several embedded charts, one of which I enable users to update with the following macro:

Code:
Private Sub Rectangle55_Click()
    With ActiveSheet
        .Calculate
        .Unprotect Password:="mypassword"
        .ChartObjects("[B]Chart 47[/B]").Chart.Axes(xlValue).CrossesAt = Range("Q497")
        .Protect Password:="mypassword", _
            UserInterfaceOnly:=True, DrawingObjects:=True, Contents:=True
        .EnableOutlining = True
    End With
End Sub

I would also like to enable users to copy that sheet, including all the charts, and to update that one particular chart with the macro. However the macro refers to specific chart reference "Chart 47", which changes when I copy the sheet, so that I get the following error:

Run-time error '1004':
Unable to get the ChartObjects property of the Worksheet class

Is there any way I can assign some alternative label to that one chart in the original sheet, which will be retained when the sheet is copied so that the macro can identify the appropriate chart in each sheet?

Please help!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I am not sure that I understand.

But if the problem is that the chart has a new name then you can read the names like the code below illustrates - obvs you have to implement the way you want. But as I said - not sure if it is the name that is the problem.


Code:
Private Sub CommandButton1_Click()
    NumCharts = ActiveSheet.ChartObjects.Count
    For i = 1 To NumCharts
        Astr = ActiveSheet.ChartObjects(i).Name
    Next i
End Sub
 
Upvote 0
Thanks, but I'm not sure we're talking about the same issue. Currently my macro relates to a chart reference "Chart 47", but a new chart reference is automatically created when the chart is copied, so my macro does not recognize the new chart. Essentially I'm looking to label the chart so that any copies of that chart can also be recognized by my macro. Does that make sense?
 
Last edited:
Upvote 0
This references the 1st chart on the active sheet irregardless of its name. Would that work for you?

Code:
.ChartObjects([COLOR="Red"]1[/COLOR]).Chart.Axes(xlValue).CrossesAt = Range("Q497")

That's what Rasm was suggesting. Count the charts on the copied sheet and then reference each one by index number.
 
Last edited:
Upvote 0
Ah, right, thanks ... but how are the charts numbered on each sheet? Is it the order they appear from top to bottom, left to right, or when they were created, or what? If I knew, I could just determine the right number directly.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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