How to convert a range to an array?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a little UDF to check the win-loss-tie records of some teams. I have the data in the table in the minisheet below. Here's the UDF code:

VBA Code:
Function WLTTally(pWLT As Range) As String

Dim WLT As Variant
WLT = Range(pWLT)

End Function

It gets a value error. Can someone tell me what I am doing wrong? Thanks

2022 NFL Power Rankings.xlsx
BCDEF
4TeamRecordWLT
5A3-030
6B2-121
7C2-121
8D1-1-1111
9E1-1-1111
10F1-212
11G1-212
12H0-303
13Total#VALUE!11112
Sheet3
Cell Formulas
RangeFormula
C13C13=WLTTally([Record])
D13D13=SUBTOTAL(109,[W])
E13E13=SUBTOTAL(109,[L])
F13F13=SUBTOTAL(109,[T])
 
That would make most code needlessly more complex, imo.
I disagree, because it means you have to check whether a range which is passed to a variant is an array or a single variant every time!!
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I disagree, because it means you have to check whether a range which is passed to a variant is an array or a single variant every time!!
I would suggest most code is not doing anything like that. It's more like if cell.value = "something" then
 
Upvote 0
I think you will find it is surprising how often this problem comes up; I posted a solution to a problem on here which the OP was very happy with solution, yet I had NOT done the check to see if the variant was an array or not specifically because it would makethe solution very complicated. However I knew the solution was not perfect. See:
XL VBA - Paste Values and preface the pasting of Values by an equal sign
I would much prefer it if EXCEL was consistent as Jennifer suggested
 
Last edited:
Upvote 0
I don’t see how that supports your argument? That code would have to be more complicated if value returned an array.
 
Upvote 0
But arrays are returned from functions all the time with only a single element, and yet they're still arrays - UBound(MyArray) = 0. That bring the case, and implicit variant arrays being what they are, it's fair to assume that a single array would be MyArray(1).

On the point about checking if an array has been initialized or not, there was a very long conversation about it over on VBForums (VB6 - Returning/Detecting Empty Arrays-VBForums). Very interesting.
 
Upvote 0
I don’t see how that supports your argument? That code would have to be more complicated if value returned an array.
Yes it would be slightly more complex ie. one line is more complex with a few resize and ubounds , but it would works regardless of whether range was a single cell on not. :
the code below works when It is called from sub test ( passing an array) but it fails with a mismatch error if passing a single variant loaded from a single cell.
So dealing with the mismatch error means testing for the array and separate code to deal with the singe variant case , much more complex
VBA Code:
Public multivariant As Variant
Sub test()
Call copytovariant(Range(Cells(5, 3), Cells(8, 6)))
End Sub
Sub copytovariant(trange As Range)
multivariant = trange.Value
End Sub

Sub Pastevariant()
ActiveCell.Resize(UBound(multivariant, 1), UBound(multivariant, 2)) = multivariant
End Sub

Sub test2()
Call copytovariant(Range(Cells(5, 3), Cells(5, 3)))
End Sub
 
Upvote 0
Hardly much more complex, imo. I'd rather live with that than Range("A1").Value()(1, 1) every time I need a cell value.

On the other hand, it might have been nice if they added a ValueArray property that always returned an array. But if wishes were horses and all that.
 
Upvote 0
Hardly much more complex, imo. I'd rather live with that than Range("A1").Value()(1, 1) every time I need a cell value.

On the other hand, it might have been nice if they added a ValueArray property that always returned an array. But if wishes were horses and all that.
I like the idea of Valuearray, that would solve the problem!!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

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