storing worksheet usedrange into an Array Class

sifar786

New Member
Joined
Aug 9, 2008
Messages
15
Hi,

i am importing a workbook and storing each worksheets UsedRange into an public array. but once the code ends, the array contents are lost. i need to make sure the contents donot get lost as these array contents would be used later for some calculations.

what i understand is that a class can be used to store these arrays into it and maintain their state as long as this workbook is open. so the array contents wont get lost.

does anyone know how to write such a class?

In Worksheet button code:
Code:
Public Sub ImportWorksheets()
Filename=GetOpenFileName(""....)
....
....
....
arrInfo = ImportWorksheetFromExcel(Filename, "WkSheet_Infomation")
....
....
....
End Sub
In a Module:
Code:
Public arrInfo() as Variant

Function ImportWorksheetFromExcel(sWorkbookPath As String, Optional sSheetName As String = "") As Variant
'Importing data from a particular sheet in a workbook into an array

    
    On Error Resume Next
    'Check if file exists
    If Len(Dir$(sWorkbookPath)) > 0 Then
            On Error GoTo ErrFailed
            
            Set oWorkbook = Application.Workbooks(wkName)
            
            'Add sheet to store results
            If Len(sSheetName) > 0 Then
                Set oWkSheet = oWorkbook.Sheets(sSheetName)
            Else
                'Just use first sheet
                Set oWkSheet = oWorkbook.Sheets(1)
            End If
            
            'Get used range after resetting UsedRange
            Dim a
            a = oWkSheet.UsedRange.Rows.Count
            Call RangeToArray(oWkSheet.UsedRange, avValues)
    
    End If
    
    ImportWorksheetFromExcel = avValues
    Exit Function

ErrFailed:
    Debug.Assert False
    Debug.Print Err.Description
    On Error GoTo 0
End Function

Function RangeToArray(rngInput As Object, avValues As Variant) As Boolean
'Reads used range values into an array
    On Error GoTo ErrFailed
    avValues = Empty
    avValues = rngInput.Value
    RangeToArray = True
    
    Exit Function

ErrFailed:
    'Failed
    Debug.Print "Error in RangeToArray: " & Err.Description
    Debug.Assert False
    RangeToArray = False
    On Error GoTo 0
End Function
The above code puts the worksheets UsedRange into an array. but the arrays go empty as soon as the ImportWorksheets() procedure ends.

Any ideas how to implement the above code into a class for storing/retrieval of worksheet data?

Any help would be most appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,224,574
Messages
6,179,629
Members
452,933
Latest member
patv

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