Variant Array issue when range only has 1 cell?

Temeraire24

New Member
Joined
Jul 13, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I just registered here at Mr. Excel, so if I am posting my question incorrectly please advise.

The following section of code works perfectly as long as the range has more than 1 row, however I get a "error 13; Type mismatch" if the range in my array only has 1 row.
How do I correct this so that if the range only has 1 row it does not error?


Dim MyArray() As Variant
Dim ptclassrow As Long
ptclassrow = Sheets("Instructions").Cells(Rows.Count, ptclasscolumn).End(xlUp).Row
MyArray = Sheets("Instructions").Range(Cells(2, ptclasscolumn), Cells(ptclassrow, ptclasscolumn)).Value2
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi
welcome to forum

Try adding line shown & see if resolves your issue

Rich (BB code):
Dim MyArray() As Variant
Dim ptclassrow As Long
ptclassrow = Sheets("Instructions").Cells(Rows.Count, ptclasscolumn).End(xlUp).Row
MyArray = Sheets("Instructions").Range(Cells(2, ptclasscolumn), Cells(ptclassrow, ptclasscolumn)).Value2
If Not IsArray(MyArray) Then MyArray = Array(MyArray)

Dave
 
Upvote 0
@Dave,

Couple of possible problems with your suggested solution. First, the array that the Array function creates is one-dimensional whereas the array created when a multi-cell range is assigned to a Variant is always two-dimensional. Second, unless Option Base 1 is used, the array created by the Array function will be zero-based whereas an array created by assigning a range to a Variant is always one-based (for each dimension).
 
Upvote 0
I would also recommend using the code like this
VBA Code:
   Dim MyArray As Variant
   Dim ptclassrow As Long
   With Sheets("Instructions")
      ptclassrow = .Cells(Rows.Count, ptclasscolumn).End(xlUp).Row
      MyArray = .Range(.Cells(2, ptclasscolumn), .Cells(ptclassrow, ptclasscolumn)).Value2
   End With
Otherwise you will get an error if the Instructions sheet is not active when the code is run.
 
Upvote 0
@Dave,

Couple of possible problems with your suggested solution. First, the array that the Array function creates is one-dimensional whereas the array created when a multi-cell range is assigned to a Variant is always two-dimensional. Second, unless Option Base 1 is used, the array created by the Array function will be zero-based whereas an array created by assigning a range to a Variant is always one-based (for each dimension).

Thanks & Agree but OP declared range having just one Cell & I sort of guessed that may be using array to load a control like combobox which if this is the case, suggestion should resolve the error but until OP responds - just a guess Rick.

Dave
 
Upvote 0
Hi & welcome to MrExcel.
Change the declaration to
VBA Code:
Dim MyArray As Variant

Thank you!! This simple change worked

Thanks & Agree but OP declared range having just one Cell & I sort of guessed that may be using array to load a control like combobox which if this is the case, suggestion should resolve the error but until OP responds - just a guess Rick.

@dmt32 Simply using @Fluff suggestion worked:
Dim MyArray As Variant instead of Dim MyArray () As Variant

What i was making is not always a single cell, in fact it would be less common for it to turn out to be one cell - the code was working until I happened to run it on a file where the range turned out to be one cell. The code i posted is inside a loop, it is getting that range for the array each time through the loop, because the loop is going to the next column each time (ptcolumn +1), and each column can have a variable amount of rows (ptclassrow is last row of whatever column ptcolumn is each time through the loop).
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,681
Members
449,328
Latest member
easperhe29

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