+ Reply to Thread
Results 1 to 13 of 13

Using Classes In Multiple Projects

Hybrid View

JackL Using Classes In Multiple... 09-22-2008, 07:45 AM
Andy Pope You need to read the... 09-22-2008, 07:54 AM
JackL thanks Andy 09-22-2008, 08:20 AM
JackL I have a new problem now that... 09-22-2008, 08:39 AM
Andy Pope Did you include a reference... 09-22-2008, 08:44 AM
JackL No - please let me know how... 09-22-2008, 08:57 AM
Andy Pope Tools > References The... 09-22-2008, 08:58 AM
JackL Thanks!!!!!!!!!!! 09-22-2008, 09:20 AM
JackL I have a new problem. The... 09-23-2008, 05:57 AM
Andy Pope What object? Can you post... 09-23-2008, 06:49 AM
JackL Please see attached files. ... 09-23-2008, 07:47 AM
Andy Pope You need to pass the... 09-23-2008, 08:08 AM
JackL Thanks for all your help! 09-23-2008, 08:19 AM
  1. #1
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20

    Using Classes In Multiple Projects

    Hi

    I am trying to replicate the class example on Chip Pearson's site:

    http://www.cpearson.com/excel/Classes.aspx

    In particular I am interested in having a class that can be accessed by multiple workbooks.

    Please can you let me know why this will not compile?

    Thanks for your help!
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    You need to read the instruction carefully.

    Back in the project containing the class, create a procedure that will create and return a new instance of the class:
    The GetClass function needs to be in a module not the class.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    thanks Andy

  4. #4
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    I have a new problem now that is when I try to initialise the class in a different workbook it will not compile saying user defined type not defined.

    Any idea what I have forgotten to do?

    Thanks again

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Did you include a reference to the project with the class in it?

  6. #6
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    No - please let me know how to reference it.

    Thanks

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Tools > References
    The project name should be in the list.

  8. #8
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    Thanks!!!!!!!!!!!

  9. #9
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    I have a new problem. The module that is using the class saved in another workbook has an object that needs to be available to the class. The project will not compile because the class cannot reference the object error: variable not defined.

    The object is set as public so I don't know what else to try. Is it even possible?

    Thanks for your help
    Jack

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    What object?

    Can you post your files. Both the one holding the class and the one trying to use it.

  11. #11
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    Please see attached files. It won't compile due to the 'fund' and 'ccy' objects not being available to the class.

    Thanks...
    Attached Files Attached Files

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    You need to pass the information to the class. It can not "see" the variables in the other project.

    Add to new Set statements to the class.
    Make the assignments in your main project.
    Amend code in class to handle local references.

    project code
    Public fund As Object, ccy As Object
    Private rr As Integer
    Sub refresh()
    Dim t As Long
    Dim CCYbatch As JVProject.CJVccy
    
    t = Timer
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        
        With Sheets("Sheet1")
            
            rr = 2
            Do Until rr > 30000
            
                Set fund = .Cells(rr, 12)
                Set ccy = .Cells(rr, 5)
                
                If fund = "" Then
                    Exit Do
                End If
                
                Application.StatusBar = "Populating fund: " & fund & " currency: " & ccy
                
                Set CCYbatch = JVProject.GetClass()
                
                Set CCYbatch.fund = fund
                Set CCYbatch.ccy = ccy
                CCYbatch.Purchase_Bonds = 0
                .Cells(rr, 12).Offset(2, -3) = CCYbatch.Purchase_Bonds
                rr = rr + 84
            Loop
        End With
        
        .StatusBar = False
        .Calculation = xlCalculationAutomatic
    End With
    t = Timer - t
    If t < 0 Then t = t + 1440
    MsgBox Format("Macro took " & t, "MM:SS") & " seconds", , "Complete!"
    End Sub
    Class code
    Private r As Integer
    Public pPurchase_Bonds As Double
    Private m_rngFund As Range
    Private m_rngCCY As Range
    
    Public Property Set Fund(RHS As Range)
        Set m_rngFund = RHS
    End Property
    Public Property Set CCY(RHS As Range)
        Set m_rngCCY = RHS
    End Property
    
    
    Public Property Get Purchase_Bonds() As Double
    
    Purchase_Bonds = pPurchase_Bonds
    
    End Property
    Public Property Let Purchase_Bonds(Value As Double)
    r = 2
    With Sheets("PnS_DL")
        Do Until .Cells(r, 52) = m_rngFund
        r = r + 1
        Loop
        Do While .Cells(r, 52) = m_rngFund
            If .Cells(r, 6) = "FIXED INCOME" And .Cells(r, 43) = "B" And .Cells(r, 64) = m_rngCCY Then
                Value = Value + .Cells(r, 70)
            End If
        r = r + 1
        Loop
    End With
        pPurchase_Bonds = Value
    End Property
    You should also add objects to allow referencing to local sheets.
    Otherwise writing a class with specific sheet references and hoping the active workbook is relevant sort of defeats the point of using a class across projects. You might as well copy the whole class code over.

  13. #13
    Registered User
    Join Date
    09-11-2008
    Location
    London
    Posts
    20
    Thanks for all your help!

+ 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. Pasting multiple lines of text into one cell
    By hoopz in forum Excel General
    Replies: 4
    Last Post: 07-11-2012, 08:28 PM
  2. spreadsheet for multiple users
    By evanzo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2008, 11:04 PM
  3. Multiple charts over multiple sets of data over multiple worksheets.
    By matrocka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-12-2007, 10:01 AM
  4. Count unique logs with multiple conditions of multiple sheets
    By Robert Bob in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-12-2007, 12:49 AM
  5. AAAaaahhhh Multiple Sheets Multiple Criteria
    By Hippychic in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-07-2006, 11:32 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