Redim an Array

Blobajob88

New Member
Joined
Mar 27, 2020
Messages
40
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, can someone please explain to me in simple terms why you have to ReDim an array. For example

Here, I need to first declare:

Dim TopClients () As Variant
Dim Dimension1 As Long, Dimension2 As Long

Dimension1 = Range("A3", Range("A2").end(xldown)).cells.count -1
Dimension2 = Range("A2",Range("A2").End(xltoright)).cells.count -1

ReDim TopClients(0 to Dimension1, 0 to Dimension2)

Why couldn't I just go:
Dimension1 = Range("A3", Range("A2").end(xldown)).cells.count -1
Dimension2 = Range("A2",Range("A2").End(xltoright)).cells.count -1

Dim TopClients (0 to Dimension1, 0 to Dimension2) As Variant
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
10,625
The short answer is that the Dim statement requires you to use constants, and the ReDim statement allows variables. But on the other hand, you don't need to use Dim first (although I'd recommend it). See here about 2 thirds of the way down:


You could also do something like this:

VBA Code:
Sub test1()
Dim TopClients As Variant

    TopClients = Range("B3").Resize(Cells(Rows.Count, "A").End(xlUp).Row - 2, Cells(2, Columns.Count).End(xlToLeft).Column - 1).Value
End Sub

Where you define the array, and load it with data all in one line. This is a 1-based array though.
 

Blobajob88

New Member
Joined
Mar 27, 2020
Messages
40
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Thanks Eric. Seems a much simpler solution
 

Watch MrExcel Video

Forum statistics

Threads
1,122,804
Messages
5,598,168
Members
414,215
Latest member
SRGDZMCS

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
Top