Assigning data to an array

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
If I can do this:

Code:
Sub tester()
    Dim LATEST_SortSheet As String
    Dim LAST_COLUMN_NUMBER As Long
    Dim LAST_COLUMN_LETTER As String
    Dim LAST_ROW_NUMBER As Long
    Dim DATA_RANGE As Range
    Dim DATA_ARRAY()
    
    LATEST_SortSheet = Sheets("Master Input Source Run").Range("D" & Sheets("Master Input Source Run").Range("D" & Rows.Count).End(xlUp).Row).Value
    LAST_COLUMN_NUMBER = Sheets(LATEST_SortSheet).Cells.Find("*", Sheets(LATEST_SortSheet).Range("A1"), xlFormulas, xlPart, xlByColumns, xlPrevious).Column
    LAST_COLUMN_LETTER = Split(Evaluate("address(1," & LAST_COLUMN_NUMBER & ")"), "$")(1)
    LAST_ROW_NUMBER = Sheets(LATEST_SortSheet).Cells.Find("*", Sheets(LATEST_SortSheet).Range("A1"), xlFormulas, xlPart, xlByRows, xlPrevious).Row
            
    Set DATA_RANGE = Sheets(LATEST_SortSheet).Range("A2:" & LAST_COLUMN_LETTER & LAST_ROW_NUMBER)
    
    ReDim DATA_ARRAY(1 To LAST_ROW_NUMBER - 1, 1 To LAST_COLUMN_NUMBER)
    
    DATA_ARRAY = DATA_RANGE   

End Sub

Why can't I do it directly like this?
DATA_ARRAY = Sheets(LATEST_SortSheet).Range("A2:" & LAST_COLUMN_LETTER & LAST_ROW_NUMBER)

I get a type mismatch error.


Thanks in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Thanks but I get the same error. Here's the amended code.

Code:
Sub tester()
    Dim LATEST_SortSheet As String
    Dim LAST_COLUMN_NUMBER As Long
    Dim LAST_COLUMN_LETTER As String
    Dim LAST_ROW_NUMBER As Long
    Dim DATA_RANGE As Range
    Dim DATA_ARRAY()
    
    'Create Array from Data in the LatestSortSheet
    LATEST_SortSheet = Sheets("Master Input Source Run").Range("D" & Sheets("Master Input Source Run").Range("D" & Rows.Count).End(xlUp).Row).Value
    LAST_COLUMN_NUMBER = Sheets(LATEST_SortSheet).Cells.Find("*", Sheets(LATEST_SortSheet).Range("A1"), xlFormulas, xlPart, xlByColumns, xlPrevious).Column
    LAST_COLUMN_LETTER = Split(Evaluate("address(1," & LAST_COLUMN_NUMBER & ")"), "$")(1)
    LAST_ROW_NUMBER = Sheets(LATEST_SortSheet).Cells.Find("*", Sheets(LATEST_SortSheet).Range("A1"), xlFormulas, xlPart, xlByRows, xlPrevious).Row
            
    'Set DATA_RANGE = Sheets(LATEST_SortSheet).Range("A2:" & LAST_COLUMN_LETTER & LAST_ROW_NUMBER)
    
    'ReDim DATA_ARRAY(1 To LAST_ROW_NUMBER - 1, 1 To LAST_COLUMN_NUMBER)
    
    DATA_ARRAY = Sheets(LATEST_SortSheet).Range("A2:" & LAST_COLUMN_LETTER & LAST_ROW_NUMBER)  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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