Home |
Search |
Today's Posts |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Heath,
Your Add method should work with chart events being triggered in the Class module. However your code only creates an empty Chart sheet albeit linked to the already instantiated (up-&-running) Class. Assuming you want to Add a Worksheet.ChartObject.Chart to the already initialized Class try something like this. As before, code in "Class1" with a bit more than last time - Public WithEvents cht As Excel.Chart Private Sub cht_Select(ByVal ElementID As Long, _ ByVal Arg1 As Long, ByVal Arg2 As Long) '' look up "GetChartElement" in help MsgBox "ElementID " & ElementID & vbCr & _ " Arg1 " & Arg1 & vbCr & " Arg2 " & Arg2 End Sub Private Sub cht_Calculate() ' change some series value MsgBox "Chart calculate" End Sub Private Sub Class_Initialize() ' get rid of this MsgBox "Class initialized" End Sub ''''''''''''' In a normal module Public clCht As Class1 ' at top of module Sub SetUpCht2() Dim ws As Worksheet Set ws = Worksheets("Sheet1") Set clCht = New Class1 ' add a ChartObject to Sheet1 and assign the Chart of the ' ChartObject to the class Set clCht.cht = ws.ChartObjects.Add(50, 40, 200, 100).Chart ' adapted from VBA help, design and set source to the chart clCht.cht.ChartWizard Source:=ws.Range("A1:B2"), _ Gallery:=xlColumn, Format:=6, PlotBy:=xlColumns, _ CategoryLabels:=1, SeriesLabels:=0, HasLegend:=1 End Sub Sub CleanUp() Set clCht = Nothing End Sub Instead of Adding' to the Class, in SetUpCht() Dim myCht as Chart Set myCht = ws.ChartObjects.Add(50, 40, 200, 100).Chart design the chart to myCht & instantiate the class as above then Set clCht.cht = myCht But for the main object of your exersize, tracking & resizing chart elements, you still have quite a task. The class calculate event should warn of the possibility of resize due to value changes but resize can occur with all sorts of other user changes. Good luck, Peter T "Heath" wrote in message ... Hi Peter, thanks again - you rock. I've tried your code and that works fine, but I still have one more problem. I think I might know the cause, but not the solution. Your suggested code in the normal module links the chart object from the class module to an existing embedded chart in the active sheet. That is, In a normal module Public clCht As Class1 'at top of module Sub SetUpCht() Set clCht = New Class1 Set clCht.cht = ActiveSheet.ChartObjects(1).Chart End Sub What I REALLY want to do is create the chart programatically and override its drawing behaviour (which will be another can of worms altogether and may require some nasty tinkering with its Calculate method since, as your suggestion reveals, it doesn't seem to expose a Paint method the way .NET controls do). But that's a whole other issue. If I can trap any events at all, then this problem will eventually yeidl to brute force :) Now, as for creating the chart from code, I have found a way to create a chart programatically; modifying the code snippet above to Sub SetUpCht() Set clCht = New Class1 Set clCht.cht = Charts.Add End Sub does the trick, and pops up a nice new chart when it executes. I can programatically set chart type, labels, data ranges etc. However, although the chart appears, the event subs no longer seem to be triggered. I suspect that the object returned by the Charts.Add method is not linking properly to the clCht.cht withevents object. I'm wondering if Excel is creating a chart object, embedding it in the sheet, but not returning a valid reference to its instance. The problem definitely lies in the "Charts.Add" call not returning an instance that has events, whereas the "ActiveSheet.ChartObjects(1).Chart" does (except the chart has to be added manually before the call to the code is made, and I want to add the chart via code). Have you any further pearls of wisdom to cast before me? I really appreciate your help - you've been generous with your experience and time. Thanks, Heath. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
HOW DO YOU TYPE - OR + IN EXCEL WITHOUT CREATING A FORMULA? | Excel Discussion (Misc queries) | |||
Excel x,y chart type x range must be re-entered each series | Charts and Charting in Excel | |||
How to prevent Excel 2003 from automatically change chart type | Charts and Charting in Excel | |||
Excel chart type that looks like a car speedometer | Charts and Charting in Excel | |||
Adding a line Chart Type to a stacked-clustered Chart Type | Charts and Charting in Excel |