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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,223,948
Messages
6,175,580
Members
452,653
Latest member
craigje92

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