Help processing a named array in a UDF

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
1,344
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
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,520
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Code:
for each rRow in Results.Rows
Winner = rRow.Cells(1, 1)
'etc
next rRow

is the basic gist.
 

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
1,344
Office Version
  1. 365
Platform
  1. Windows
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?
 

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
1,344
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,520
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
1,344
Office Version
  1. 365
Platform
  1. Windows
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?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,520
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It's fine, albeit pointless as it stands. ;)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,451
Messages
5,528,834
Members
409,839
Latest member
akashsadhu
Top