JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,687
- Office Version
- 365
- Platform
- Windows
I would like to write a UDF that can "look up" a value in a sheet table if passed the name of the table and the search text. Here's a simplified example of what I would like to do:
Suppose I have this table in Sheet1 containing the scores in a trivia game:
[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD="align: center"]C/R[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]Amy[/TD]
[TD="align: right"]28[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]Brad[/TD]
[TD="align: right"]21[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]Chris
[/TD]
[TD="align: right"]35[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD]Dave[/TD]
[TD="align: right"]12[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD]Emily[/TD]
[TD="align: right"]19[/TD]
[/TR]
</tbody>[/TABLE]
The range $C$4:$D$8 has the global name "TriviaScores".
I would like to write a UDF that can "look up" a player's score if passed the name of the player and the name of the table.
The UDF needs to be able to be called from any sheet in the workbook, not necessarily the sheet where the table is.
This code seems to work.
For example, if a cell contains the expression,
The result in that cell would be "35". The "35" needs to be a numeric value (Double).
A few questions:
Suppose I have this table in Sheet1 containing the scores in a trivia game:
[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD="align: center"]C/R[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]Amy[/TD]
[TD="align: right"]28[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]Brad[/TD]
[TD="align: right"]21[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]Chris
[/TD]
[TD="align: right"]35[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD]Dave[/TD]
[TD="align: right"]12[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD]Emily[/TD]
[TD="align: right"]19[/TD]
[/TR]
</tbody>[/TABLE]
The range $C$4:$D$8 has the global name "TriviaScores".
I would like to write a UDF that can "look up" a player's score if passed the name of the player and the name of the table.
The UDF needs to be able to be called from any sheet in the workbook, not necessarily the sheet where the table is.
This code seems to work.
Code:
Public Function Trivia(PlayerName As String, TableName As Range) As Variant
Dim Score As Integer
Dim NumRows As Integer
NumRows = TableName.Rows.Count 'The number of columns
Dim Rowi As Integer
For Rowi = 1 To NumRows
If UCase(PlayerName) = UCase(TableName(Rowi, 1)) Then
Trivia = CDbl(TableName(Rowi, 2))
Exit Function
End If
Next Rowi
Trivia = CVErr(xlErrName)
End Function
For example, if a cell contains the expression,
Code:
=Trivia("Chris","TriviaScores")
The result in that cell would be "35". The "35" needs to be a numeric value (Double).
A few questions:
- Is there a more efficient way than looping through each row? These tables are going to have just a few rows (<10).
- Do I need the CDbl function? It seems to work without it.
- If I change the UDF to Double (from Variant), it returns a Value error instead of the Name error. Why?