VBA Does declaring Array range with Cell make it a 2-Dimensional array?

Lbdch

New Member
Joined
Apr 25, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Might be a dumb question, but I have used the following to create an array in VBA -

VBA Code:
  Dim CurrMonth As String
     Dim PosMatch As Variant
     Dim MonthDayCount As Variant
     Dim MonthDays As Variant
     
     CurrMonth = Format$(Date, "MMMM")
     
     With Worksheets("Tracker")
     
     PosMatch = Application.Match(CurrMonth, .Range("A:A"), 0)
     
     MonthDayCount = .UsedRange.Rows(PosMatch).Columns.Count - 2
     
     ReDim MonthDays(1 To MonthDayCount)
     MonthDays = .Range(.Cells(PosMatch, 2), .Cells(PosMatch, MonthDayCount))

The array [Month Days] is output as a 2-Dimensional array, but since the information that is input into the array comes from a row it essentially makes a 2-Dimensional array one column wide. Why? How do I change it to a 1-Dimensional array so I don't have to solve for a 2-Dimensional array later on in the code?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
When you load an array directly from a sheet it is always a 2D array, try
VBA Code:
     MonthDays = Application.Index(.Range(.Cells(PosMatch, 2), .Cells(PosMatch, MonthDayCount)).Value, 1, 0)
You can also get rid of this line
VBA Code:
 ReDim MonthDays(1 To MonthDayCount)
 
Upvote 0
Solution
When you load an array directly from a sheet it is always a 2D array, try
VBA Code:
     MonthDays = Application.Index(.Range(.Cells(PosMatch, 2), .Cells(PosMatch, MonthDayCount)).Value, 1, 0)
You can also get rid of this line
VBA Code:
 ReDim MonthDays(1 To MonthDayCount)
It works, but why?
 
Upvote 0
If you index a single row, it will return a 1D array.
 
Upvote 0

Forum statistics

Threads
1,216,767
Messages
6,132,599
Members
449,738
Latest member
brianm3y3r

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