Help processing a named array in a UDF

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
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:

R/CCDEFG
13WinnerLoserSet1Set2Set3
14AC6-34-66-2
15BD6-17-5
16DE6-46-4
17CE3-67-57-5
18AB6-16-4
19BD7-67-6

<tbody>
</tbody>

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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
for each rRow in Results.Rows
Winner = rRow.Cells(1, 1)
'etc
next rRow

is the basic gist.
 
Upvote 0
Code:
for each rRow in Results.Rows
Winner = rRow.Cells(1, 1)
'etc
next rRow

is the basic gist.

Great, thanks. Is rRow a variable or a keyword? If it's a variable, how do I declare it?
 
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,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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