Quick array question

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
238
Can anyone give e the syntax of how I'd create a variant array without setting the dimensions beforehand or using a range on a sheet

At the moment I basically
Code:
[B]Dim unclosed_liabilities_array As Variant[/B]
then set the array by finding the last row and selecting an empty space on the sheet
Code:
[B]unclosed_liabilities_array = sht_runners.Range("BB14:BA" & LrowRunners).Value[/B]

I'm guessing I need to use ReDim somewhere along the line but just need the syntax for setting the empty arrays dimensions which I assume are simply number of element and number of columns/

Thanks
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
See if this helps:
Code:
Sub M1()

    Dim LR       As Long
    Dim LC       As Long
    Dim arr()   As Variant

    Application.ScreenUpdating = False
    
    With Sheets("Sheet1")
         LR = .Cells(.Rows.count, 1).End(xlUp).row
         LC = .Cells(1, .Columns.count).End(xlToLeft).column
        arr = .Cells(1, 1).Resize(LR, LC).Value
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Thanks I was hoping it'd be something simpler like below as I've seen ReDim in lots of coding here and there with regard to arrays. I basically now how many columns I'd want and the only variable would be how many records the array held.

Code:
ReDim unclosed_liabilities_array(2,LrowRunners)
 
Upvote 0
You could create an undimensioned variant simply by re lading it.
Code:
Dim unclosed_liabilities_array() As Variant
 
Last edited:
Upvote 0
If you know the column count (e.g. 10), then you could adapt to:
Rich (BB code):
Sub M1()

    Dim LR       As Long
    Dim arr()   As Variant

    Application.ScreenUpdating = False
    
    With Sheets("Sheet1")
         LR = .Cells(.Rows.count, 1).End(xlUp).row
        arr = .Cells(1, 1).Resize(LR, 10).Value
    End With
    
    Application.ScreenUpdating = True
    
End Sub
If you're reading a fixed number of columns with varying number of rows, I'm not sure why you'd to "pre" ReDim your array size, as you're still going to write the data to the array, like your example:
unclosed_liabilities_array = sht_runners.Range("BB14:BA" & LrowRunners).Value
Also, should that be "BB14:BA" & LrowRunners or "BA14:BB" & LrowRunners, given column BA is before BB?
 
Last edited:
Upvote 0
If you know the column count (e.g. 10), then you could adapt to:
Rich (BB code):
Sub M1()

    Dim LR       As Long
    Dim arr()   As Variant

    Application.ScreenUpdating = False
    
    With Sheets("Sheet1")
         LR = .Cells(.Rows.count, 1).End(xlUp).row
        arr = .Cells(1, 1).Resize(LR, 10).Value
    End With
    
    Application.ScreenUpdating = True
    
End Sub
If you're reading a fixed number of columns with varying number of rows, I'm not sure why you'd to "pre" ReDim your array size, as you're still going to write the data to the array, like your example:

Also, should that be "BB14:BA" & LrowRunners or "BA14:BB" & LrowRunners, given column BA is before BB?

The BB:BA was a typo as I amended it on the fly for the purposes of a two column array

Thanks for your help , I thought there may be a simple way to get an empty array with set dimensions as it gets populated later but needs to be a set numbers of elements generated LrowRunners
 
Upvote 0
You could create an undimensioned variant simply by re lading it.
Code:
Dim unclosed_liabilities_array() As Variant

Thanks I had used that before but I'd want to set the array elements using LrowRunners to match another array size used to populate it rather than adding to the empty array as I went along, maybe the method I use at the moment is the simplest for now.
 
Last edited:
Upvote 0
I'm confused, if you want an empty array with set dimensions just declare one.
Code:
Dim unclosed_liabilities_array(1 To 10, 1 To 20) As Variant
 
Upvote 0
There may be confusion with what you're trying to do and use of dimensionalised arrays.

ReDim with PRESERVE tends to be used when you're creating a array with respect to another array or data process for output where the size is unknown (e.g. filtering values from an input array to an output array).

I've not seen many situations where you need to initially set the size of the array, even if copying from an input array to output array, but that's not saying it doesn't exist either.
 
Upvote 0
I'm confused, if you want an empty array with set dimensions just declare one.
Code:
Dim unclosed_liabilities_array(1 To 10, 1 To 20) As Variant

I need an empty array but the dimensions aren't set , the number of elements will be set using the value in LrowRunners, I was looking for the syntax to set an array with LrowRunners elements in it I had tried
Code:
Dim unclosed_liabilities_array(1 To LrowRunners) As Variant
but it gives constant errors
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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