Redim an Array

Blobajob88

Board Regular
Joined
Mar 27, 2020
Messages
55
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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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