How to convert a range to an array?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,543
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])
 

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.
(1) You've defined pWLT as Range, so you're trying to get the values of a Range(Range))..
(2) Your return data type is a string. If you want an array, it shpuld at least be a Variant.
(3) You need to assign the return are to rhe Function name.
So:

VBA Code:
Function WLTTally(pWLT As Range) As Variant
    WLTTally = pWLT.Value
End Function
 
Upvote 0
Solution
(1) You've defined pWLT as Range, so you're trying to get the values of a Range(Range))..
(2) Your return data type is a string. If you want an array, it shpuld at least be a Variant.
(3) You need to assign the return are to rhe Function name.
So:

VBA Code:
Function WLTTally(pWLT As Range) As Variant
    WLTTally = pWLT.Value
End Function
Your code works, thanks. Here's the skeleton of my UDF.

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

Dim WLT As Variant
Dim i As Integer
Dim NumTeams As Integer

WLT = pWLT.Value
NumTeams = UBound(WLT, 1)
For i = 1 To NumTeams

' . . . some code

Next i

End Function
 
Upvote 0
PS: Does it make any difference whether I define WLT with or without parens? They both seem to work.

VBA Code:
'Without ()
Dim WLT As Variant
WLT = pWLT.Value

'With ()
Dim WLT() As Variant
WLT = pWLT.Value
 
Upvote 0
The parentheses declare it as an array, so it would fail if your range were comprised of just one cell.
 
Upvote 0
The parentheses declare it as an array, so it would fail if your range were comprised of just one cell.
Really? So Excel treats a single row as a 1xN array and a single column as an Nx1 array, but a single cell passed in exactly the same way is a scalar, and not a 1x1 array?

I would love to hear the (il)logic behind that implementation.
 
Upvote 0
Really? You’d expect reading a single cell value to give you an array?
 
Upvote 0
Really? You’d expect reading a single cell value to give you an array?
Yes, really, absolutely! It all depends on how other shapes are treated.

If A1:B3 is treated as a 3x2 array (2 dimensions), A1:A3 is treated as a 1-dimensional vector of length 3, and A1:D1 is treated as a 1-dimensional vector of length 4, then A1 (or A1:A1) would be a scalar (0 dimensions).

But that's not how it is. A1:B3 is treated as a 3x2 array (2 dimensions), but A1:A3 is also treated as a 2-dimensional array (3x1) and A1:D1 is treated as a 2-dimensional array (1x4 array). Therefore, A1 (or A1:A1) has to be a 1x1 array.

I understand why they want to treat single rows and columns as arrays. That's fine. But then a range that is both a single row and a single column (a cell) should be a 1x1 array.
 
Last edited:
Upvote 0
That would make most code needlessly more complex, imo.
 
Upvote 0
That would make most code needlessly more complex, imo.
Perhaps. That would depend on the overall design and implementation. A coherent, consistent design would actually be simpler and far more consistent than the cobbled-together, piecemeal mess that we have now. VBA is pretty widely viewed as one of the worst programming languages ever. It's far worse that Fortran or Cobol, two of the earliest programming languages. It's even worse than VB, which is right there at the bottom of the pile, too.

But you did not respond to my point about consistency. 😉
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,840
Members
449,411
Latest member
adunn_23

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