Help processing a named array in a UDF

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I would like to write a UDF that will examine the contents of each cell in a named array and return various results. The array contains the scores of tennis matches between players on a club tennis ladder. Here's an example:

[TABLE="class: grid, width: 349"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[/TR]
[TR]
[TD="align: center"]13[/TD]
[TD="align: center"]Winner[/TD]
[TD="align: center"]Loser[/TD]
[TD="align: center"]Set1[/TD]
[TD="align: center"]Set2[/TD]
[TD="align: center"]Set3[/TD]
[/TR]
[TR]
[TD="align: center"]14[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]6-3[/TD]
[TD="align: center"]4-6[/TD]
[TD="align: center"]6-2[/TD]
[/TR]
[TR]
[TD="align: center"]15[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]6-1[/TD]
[TD="align: center"]7-5[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]16[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]6-4[/TD]
[TD="align: center"]6-4[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]17[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]3-6[/TD]
[TD="align: center"]7-5[/TD]
[TD="align: center"]7-5[/TD]
[/TR]
[TR]
[TD="align: center"]18[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]6-1[/TD]
[TD="align: center"]6-4[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]19[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]7-6[/TD]
[TD="align: center"]7-6[/TD]
[TD="align: center"][/TD]
[/TR]
</tbody>[/TABLE]

Assume that the range C14:G19 is assigned the name "Results", I would like help writing the skeleton of a loop that will allow me to process the data in each row. I envision something like this:
Code:
Function Tally(Results As Range, Options As String)

...initial code

For Each Row In Results
  ...initial code for each row
  Winner = ???
  Loser = ???
  Set1 = ???
  Set2 = ???
  Set3 = ???
  ...calculations on data
Next Row

...cleanup & results

End Function

Can someone help me with the correct syntax for the For loop and for accessing the contents of the cells inside the loop?

The Options parameter will tell the UDF what results to return.

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
for each rRow in Results.Rows
Winner = rRow.Cells(1, 1)
'etc
next rRow

is the basic gist.
 
Upvote 0
It's a Range object variable.

That seems to work. Thanks.

I forgot one thing. The named range, "Results", actually includes a header row at the top and a summary row at the bottom that I want to skip over. Do I change the For loop to go from 2 to n-1 and, if so, what's the syntax for that?

Thanks
 
Upvote 0
You can either loop using something like:

Code:
for n = 2 to results.rows.count - 1
Set rRow = results.rows(n)

or combine offset and resize to get the range you need and then use the original For Each loop.
 
Upvote 0
You can either loop using something like:

Code:
for n = 2 to results.rows.count - 1
Set rRow = results.rows(n)

or combine offset and resize to get the range you need and then use the original For Each loop.

Great. This code seems to be working.

Code:
Function TallyResults(Results As Range, Options As String)

Dim rRow As Range
Dim Winner, Loser, Set1, Set2, Set3 As String
Dim iRow As Integer

For iRow = 2 To Results.Rows.Count - 1
  Set rRow = Results.Rows(iRow)
  Winner = rRow.Cells(1, 1)
  Loser = rRow.Cells(1, 2)
  Set1 = rRow.Cells(1, 3)
  Set2 = rRow.Cells(1, 4)
  Set3 = rRow.Cells(1, 5)
Next iRow

Any comments?
 
Upvote 0
It's fine, albeit pointless as it stands. ;)
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
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