Trouble with a class module / hyperlinking chart objects

mrjakob

New Member
Joined
Apr 13, 2015
Messages
9
Hi everyone,

I'm new to the forum and this is my first post. I've tried to look for existing answers to my questions but either they don't exist or I just don't understand the answers. English is not my native tongue, so please bear with me if I dont express my self correctly.

The challenge:
I need to get the individual "pies" inside a pie chart to link to different webpages.

I'm definately not "fluent" in VBA, i'm kind of a beginner - so I browsed the web and someone came up with this:

Rich (BB code):
Option Explicit

Public WithEvents CHT As Chart

Private Sub Workbook_Open()
    Set CHT = ActiveSheet.ChartObjects(1).Chart
End Sub

Private Sub CHT_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    On Error GoTo Fin
    If Selection.Name = "series1" Then
Range("A1").Select<---- I'm putting the hyperlink in this cell
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
    End If
Fin:
End Sub
But I'm not sure if I am using it correctly. I'm placing it inside a class module, and named the data for my chart 'series1', but I keep ketting the error message:

"Cannot run the macro 'chart.xlsm!Diagram2_Click'. The macro may not be available in this workbook or all macros may be disabled."

And I'm allowing all macros... Do anyone of you know what I am doing wrong?

Any help is greatly appreciated :)

Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Did you assign a macro to your chart?
 
Upvote 0
Hi RoryA,

Thanks for your reply.

I've right clicked the chart --> clicked assign macro --> selected the macro --> pressed OK.

It seems to have assigned correctly because when I hover the mouse over the chart, my arrow-marker turns into a finger-marker. But when I click it it shows the error message I posted above.
 
Upvote 0
You actually shouldn't assign a macro to the chart. The code you posted above takes care of everything automatically.
 
Upvote 0
Now I've deleted the chart and insert a new one.
The code doesn't run at all now when I click the chart and the individual objects inside the chart.

Any ideas on what I can do to make it work?
 
Upvote 0
You either need to save,close and reopen the file, or manually run the Workbook_Open code. The code is only hooked up to the chart when the workbook is opened.
 
Upvote 0
Thank you for your reply.

I saved and reopened and it still doesn't run. I've tried to run it manually but when I try it pops up like in this screenshot.



Am I doing something wrong?
 
Upvote 0
Oh I see - that code needs to all be in the ThisWorkbook module, not in your own class module.
 
Upvote 0
Alright :)

Now I tried that and it seems like I'm getting closer now - with your help (which is much appreciated!).

When I copy it into the ThisWorkbook module, save and reopen and click the object in the chart, then a pop up like this shows up:



And when I click OK the yellow line is highlighted (I painted that on to show you).
 
Upvote 0
Your second If should be an ElseIf
 
Upvote 0

Forum statistics

Threads
1,213,537
Messages
6,114,216
Members
448,554
Latest member
Gleisner2

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