+ Reply to Thread
Results 1 to 5 of 5

Adding new method or property to existing Classes (like Range)

  1. #1
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Adding new method or property to existing Classes (like Range)

    Hello Dear Excel Help Forum

    Is it possible to extend default classes?
    I know I can extend ThisWorkbook class.

  2. #2
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: Adding new method or property to existing Classes (like Range)

    Workbook: Declare variables and add methods and properties in the workbook code module

    Objects like range: Create a class named, say, ExtRng, declare its public variable, say, rng As Range, and write its methods and properties. When you want to work with an "extended range object", do it this way:

    Please Login or Register  to view this content.
    and you can use methods and properties of eR; your original range is called as eR.rng.

  3. #3
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Adding new method or property to existing Classes (like Range)

    Jan thank you. This would suffice. Though being able to extend default classes without the need of declaring new class would be more convenient.

  4. #4
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: Adding new method or property to existing Classes (like Range)

    I agree it would be more convenient. Maybe, simple tricks exist but I don't know any. The first thing to come in mind is to use inheritance but it is sooo awful in VBA...

    The new-class trick possesses some advantages:

    1. If you frequently use, say, Range.Interior.ColorIndex, you can shorten it in the ExtRng class with your own property in the class code body:
    Please Login or Register  to view this content.
    and wite
    eR.rClrI = ...
    instead of
    the_range.Interior.ColorIndex = ...

    2. You can override the range default property (Value) with the property you like. For instance, you simply add a line to the upper code:
    Please Login or Register  to view this content.
    which makes the property (both Get and Let parts) the default one, and you can write eR=... instead of eR.rClr=... then. The best idea is not to make a property, but the variable rng default:
    Please Login or Register  to view this content.
    and use simple eR instead of eR.rng then.

    The lines 'Attribute...' must be written in an external editor; if you write them in VBA, they won't be recognized. Export the class from workbook, add the 'Attribute...' line, say, via the NotePad, save it and import it back into the workbook.

  5. #5
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Adding new method or property to existing Classes (like Range)

    Nice, maybe someday I will write a routine for exporting adding line and importing it again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Classes - Public Property Set Confusion!
    By reyreyreyes in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-22-2016, 08:24 PM
  2. Replies: 1
    Last Post: 11-07-2015, 09:31 AM
  3. help with adding new names and classes to my spreadsheet.
    By Keyboard Ninja in forum Excel General
    Replies: 6
    Last Post: 03-26-2015, 07:48 PM
  4. [SOLVED] Autofilter method of range class failed despite data match existing
    By yoda66 in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 12-06-2013, 06:44 AM
  5. Adding objects as a property of cells/range
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2013, 03:12 AM
  6. Need help, adding data series to existing chart using SeriesCollection.Add Method?
    By ath1337 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-05-2012, 05:29 PM
  7. Adding a new row into an existing named range in a macro
    By topper in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2009, 10:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1