VBA Classes to add properties

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,416
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm currently programming a macro for which it would be handy to expand the current Excel properties of an object. I'd like e.g. to expand the ChartArea object with a new property named "Country" (string) and an property named "QualityPercentage" (double). I've been messing around a bit with classes, but wonder if it's possible at all to do something like that (with or without classes)?

Hope someone has a clue?

Cheers,

Koen
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You can handle chart's events in class.

1. Create some class (call it ExtendedChart).
2. Create public variable in this class (call it Cht): Public WithEvents Cht As Chart.
3. Create standard module.
4. In this module create variable: Public ExChart As ExtendedChart.
5. In Workbook_Open event type in (change chart to appropriate):
Code:
Set ExChart = New ExtendedChart
ExChart.Cht = Sheets("Sheet1").ChartObjects(1).Chart
In class you can create any propperty and use in chart's events.
 
Upvote 0
Based on Sektor's suggestion you can add the Properties as follows : ( In the Class Module)

Code:
Public WithEvents Cht As Chart

Private sCountry As String
Private dQualityPercentage As Double


Public Property Get Country() As String
    Country = sCountry
End Property

Public Property Let Country(ByVal vNewValue As String)
    sCountry = vNewValue
End Property

Public Property Get QualityPercentage() As Double
    QualityPercentage = dQualityPercentage
End Property

Public Property Let QualityPercentage(ByVal vNewValue As Double)
    dQualityPercentage = vNewValue
End Property

Then you could refer to them in code like :

Code:
Private ExChart As ExtendedChart

Sub test()

    Set ExChart = New ExtendedChart
    Set ExChart.Cht = Sheets(1).ChartObjects(1).Chart
    
    ExChart.Country = "UK"
    MsgBox ExChart.Country

End Sub
 
Upvote 0
Neat, thanks guys, this should get me going :)!
 
Upvote 0
Hi guys,
I have the next challenge :)... So I'm able to make this class and it works like a charm. The issue I have: can I save the new object somehow (so I can close the file and open it again and it's still there)?
I mean: standard chart objects are saved with all their properties, but my extra 2 properties are in my class. I can store them in a worksheet or in an external .txt file (and load them upon opening, based on e.g. the name I give the chart object), but the preferable option would be to save it in the "extended chart" object in the file. Is that possible (and if so, how)?
Cheers,
Koen
 
Upvote 0
Hi guys,
I have the next challenge :)... So I'm able to make this class and it works like a charm. The issue I have: can I save the new object somehow (so I can close the file and open it again and it's still there)?
I mean: standard chart objects are saved with all their properties, but my extra 2 properties are in my class. I can store them in a worksheet or in an external .txt file (and load them upon opening, based on e.g. the name I give the chart object), but the preferable option would be to save it in the "extended chart" object in the file. Is that possible (and if so, how)?
Cheers,
Koen

The class instances are lost when the workbook is closed or the variables go out of scope as you know.

If you don't want to have the Class object Properties saved to disk or in a worksheet, you may want to save them in workbook Names and then retrive their values by evaluating the names.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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