VBA: Copy range to 2D Array

blothian

Board Regular
Joined
Mar 17, 2016
Messages
53
As a continuation of a current macro I'm writing ( read 1st post here http://www.mrexcel.com/forum/excel-...ect-invoked-has-disconnected-its-clients.html for brief explanation).

Now that I can access the content in the workbook's worksheets, I have written a simple function to calculate the last row in a worksheet:

Code:
Public Function CountChainageRows(ws As Excel.Worksheet) As Long
' ---------------------------------------------------------------------
'   Find the number of the last row in the chainage column
' ---------------------------------------------------------------------
    CountChainageRows = 0


    CountChainageRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    FrmPWayLevel.TB_Chainage.Value = CountChainageRows
End Function

I use this in a function which has a purpose of copy data from certain columns into a 2D array like so:

Code:
Public Function ReadWorkSheet(ByRef data() As String)
' ---------------------------------------------------------------------
'   Copy cells from selected worksheet to 2D array
' ---------------------------------------------------------------------
    Dim R                                    As Long
    Dim C                                    As Long
    
    ReDim data(0)
    data = OCurrentWs.Range("C3:C & CountChainageRows(OCurrentWs)")
    For R = 1 To UBound(data, 1)    ' First array dimension is rows.
        For C = 1 To UBound(data, 2)    ' Second array dimension is columns.
            Debug.Print data(R, C)
        Next C
    Next R
End Function

OCurrentWs is a public variable and its value is set each time a listbox item is clicked and in turn ReadWorkSheet would then be called. However, the line data = OCurrentWs.Range("C3:C & CountChainageRows(OCurrentWs)") is giving the error Method 'Range' of object '_worksheet' failed. I've put a break in the line before it errors and can see OCurrentWs has a value assigned to it so I'm unsure what else is wrong.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You need to read the range into a Variant:

Code:
Public Function ReadWorkSheet(ByRef [COLOR="#FF0000"]data As Variant[/COLOR])
' ---------------------------------------------------------------------
'   Copy cells from selected worksheet to 2D array
' ---------------------------------------------------------------------
    Dim R                                    As Long
    Dim C                                    As Long
    
    data = OCurrentWs.Range("C3:C & CountChainageRows(OCurrentWs)").Value2
 
Upvote 0
Hi,
Try changing the line as follows

Code:
data = OCurrentWs.Range("C3:C" & CountChainageRows(OCurrentWs)).Value

Data parameter should be a variant

Dave
 
Last edited:
Upvote 0
Thanks for the replies and spotting the quotation mark position error.
Is there a specific reason why an array of strings doesn't work? I've used variant arrays in other macro's but can't recall why I did at the time.
 
Upvote 0
Because the Value (or Value2) property of a multi-cell range returns a variant array, and that can only be assigned to a Variant.

WorksheetFunction.MMult returns the products of two matrices, which would always be an array of Doubles, but you still can't assign it to a dynamic array or type Double, just because the function returns the array as a Variant.
 
Upvote 0

Forum statistics

Threads
1,215,230
Messages
6,123,752
Members
449,118
Latest member
kingjet

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