Results 1 to 16 of 16

Thread: How to create a public array in class module?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,894
    Location
    1. That's a lot of hard coded numbers to have in a Class. Personally, I'd find 100 individually named arrays (srf1 … srf100) cumbersome to work with

    I used Bob's movem sub to put into WS and doubled to get 100 cols, since personally I think that's the best place for 100K+ numbers


    2. In a standard module

    
    Option Explicit
    Sub test()
        Dim A As clsArray
        
        Set A = New clsArray
        
        With A
        
            Call .Init(Worksheets("Sheet1").Cells(1, 1).CurrentRegion)
            
            MsgBox .ColVector(2)(2)
        
            MsgBox Application.WorksheetFunction.Max(.ColVector(10))
            MsgBox Application.WorksheetFunction.Min(.ColVector(20))
            MsgBox Application.WorksheetFunction.Average(.ColVector(30))
        End With
        Set A = Nothing
    
    End Sub
    
    




    3. In a class module named clsArray

    Option Explicit
    
    Dim m_Array As Range
    
    Private Sub Class_Initialize()
        'if always  the same data range, could go here
        Set m_Array = Nothing
    End Sub
    
    
    Private Sub Class_Terminate()
        Set m_Array = Nothing
    End Sub
    
    
    Sub Init(r As Range)
        
        On Error GoTo Err_Handler
        Set m_Array = r
        Exit Sub
        
    Err_Handler:
        
    End Sub
    
    Property Get ColVector(n As Long) As Variant
        ColVector = Empty
        
        On Error GoTo Err_Handler
        ColVector = Application.WorksheetFunction.Transpose(m_Array.Columns(n))
        Exit Property
        
    Err_Handler:
        
    End Property
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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