Single cell array

aurelius89

Board Regular
Joined
Mar 15, 2017
Messages
69
I have a snippet of code below which will assign a range to an array, all works well:

Code:
Dim rngAB As Range
Dim arr() As Variant
Set rngAB = wsWeights.Range("AB4:AB" & wsWeights.Cells(Rows.count, "AB").End(xlUp).Row)
 arr = rngAB

However, when they result of the .End(xlUP).row is 4 which makes the outcome of the range "AB4:AB4" it throws up an error.
So I changed to this:

Code:
If wsWeights.Cells(Rows.count, "AB").End(xlUp).Row = 4 Then    
Set rngAB = wsWeights.Range("AB4")
    arr = rngAB
Else
    Set rngAB = wsWeights.Range("AB4:AB" & wsWeights.Cells(Rows.count, "AB").End(xlUp).Row)
    arr = rngAB
End If

Now it lets me assign the range but I cannot put that single cell range into the array. It states "Type mismatch"

What am I doing wrong?
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I have a snippet of code below which will assign a range to an array, all works well:

Code:
Dim rngAB As Range
Dim arr() As Variant
Set rngAB = wsWeights.Range("AB4:AB" & wsWeights.Cells(Rows.count, "AB").End(xlUp).Row)
 arr = rngAB

However, when they result of the .End(xlUP).row is 4 which makes the outcome of the range "AB4:AB4" it throws up an error.
So I changed to this:

Code:
If wsWeights.Cells(Rows.count, "AB").End(xlUp).Row = 4 Then    
Set rngAB = wsWeights.Range("AB4")
    arr = rngAB
Else
    Set rngAB = wsWeights.Range("AB4:AB" & wsWeights.Cells(Rows.count, "AB").End(xlUp).Row)
    arr = rngAB
End If

Now it lets me assign the range but I cannot put that single cell range into the array. It states "Type mismatch"

What am I doing wrong?
You are not doing anything wrong... apparently Excel/VBA does not consider a single cells to be an array. You can do what you show above, although (personal preference) I would pull the arr=rngAB line of code from each If..Then..Else block and put it under the End If instead. Another way to handle it would be to expand the range by one row to force an array in just a single line of code and, depending on what your code is doing, either process the blank array element or set your limits to ignore it when processing it (and assuming you have something in cell AB3, like a head, to make sure the End(xlUp) stops at AB4).
Code:
[table="width: 500"]
[tr]
	[td]arr = wsWeights.Range("AB4:AB" & wsWeights.Cells(Rows.count, "AB").End(xlUp).Row [B][COLOR="#FF0000"]+ 1[/COLOR][/B])[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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