Solved

# Unable to get the Index Property of the WorksheetFunction Class

Posted on 2014-10-23
258 Views
I am using the following code to retrieve a value from a shifting range of cells.  It worked fine on my laptop, but when I ran it on another machine, I started to get the error that states that Excel is "Unable to get the Index Property of the WorksheetFunction Class".  My laptop is running Excel 2013 and the production pc is running Excel 2007. I'm stumped.

Public Function udf_CTRequired(TempC As Double, ph As Double, Resid As Double) As Double
Dim wb As Workbook
Dim ws As Worksheet
Dim rngTable As Range
Dim TempF As Double
Dim x As Long
Dim y As Long
Dim dblResult As Variant

Set wb = ThisWorkbook
Set ws = wb.Sheets("CTValues")
TempF = (9 / 5) * TempC + 32

'Determine starting cell according to Temperature
If TempF < 40.91 Then
Set rngTable = ws.Range("C3", "I16")
rngTable.Font.Bold = False
Else
If TempF >= 40.91 And TempF < 49.91 Then
Set rngTable = ws.Range("C17", "I30")
Else
If TempF >= 49.91 And TempF < 58.91 Then
Set rngTable = ws.Range("C31", "I44")
Else
If TempF >= 58.91 And TempF < 67.91 Then
Set rngTable = ws.Range("C45", "I58")
Else
If TempF >= 67.91 And TempF < 76.91 Then
Set rngTable = ws.Range("C59", "I72")
Else
If TempF >= 76.91 Then
Set rngTable = ws.Range("C73", "I86")
End If
End If
End If
End If
End If
End If

'Determine column offset according to pH
If ph < 6.04 Then
x = 0
Else
If ph >= 6.04 And ph < 6.55 Then
x = 1
Else
If ph >= 6.55 And ph < 7.04 Then
x = 2
Else
If ph >= 7.04 And ph < 7.55 Then
x = 3
Else
If ph >= 7.55 And ph < 8.04 Then
x = 4
Else
If ph >= 8.04 And ph < 8.55 Then
x = 5
Else
If ph >= 8.55 Then
x = 6
End If
End If
End If
End If
End If
End If
End If

'Determine row offset according to chlorine residual
If Resid < 0.5 Then
y = 0
Else
If Resid >= 0.5 And Resid < 0.7 Then
y = 1
Else
If Resid >= 0.7 And Resid < 0.9 Then
y = 2
Else
If Resid >= 0.9 And Resid < 1.1 Then
y = 3
Else
If Resid >= 1.1 And Resid < 1.3 Then
y = 4
Else
If Resid >= 1.3 And Resid < 1.5 Then
y = 5
Else
If Resid >= 1.5 And Resid < 1.7 Then
y = 6
Else
If Resid >= 1.7 And Resid < 1.9 Then
y = 7
Else
If Resid >= 1.9 And Resid < 2.1 Then
y = 8
Else
If Resid >= 2.1 And Resid < 2.3 Then
y = 9
Else
If Resid >= 2.3 And Resid < 2.5 Then
y = 10
Else
If Resid >= 2.5 And Resid < 2.7 Then
y = 11
Else
If Resid >= 2.7 And Resid < 2.9 Then
y = 12
Else
If Resid >= 2.9 Then
y = 13
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

dblResult = Application.WorksheetFunction.Index(rngTable, x, y)
udf_CTRequired = dblResult
End Function
0
Question by:AaronGreene1906
• 2
• 2

LVL 27

Accepted Solution

Glenn Ray earned 500 total points
ID: 40399918
Try this revision to your code:

Public Function udf_CTRequired(TempC As Double, ph As Double, Resid As Double) As Double
Dim wb As Workbook
Dim ws As Worksheet
Dim rngTable As Range
Dim TempF As Double
Dim x As Long
Dim y As Long
Dim dblResult As Variant

Set wb = ThisWorkbook
Set ws = wb.Sheets("CTValues")
TempF = (9 / 5) * TempC + 32

'Determine starting cell according to Temperature
Select Case TempF
Case Is < 40.91
Set rngTable = ws.Range("C3:I16")
rngTable.Font.Bold = False
Case Is < 49.91
Set rngTable = ws.Range("C17:I30")
Case Is < 58.91
Set rngTable = ws.Range("C31:I44")
Case Is < 67.91
Set rngTable = ws.Range("C45:I58")
Case Is < 76.91
Set rngTable = ws.Range("C59:I72")
Case Else
Set rngTable = ws.Range("C73:I86")
End Select

'Determine column offset according to pH
Select Case ph
Case Is < 6.04
x = 0
Case Is < 6.55
x = 1
Case Is < 7.04
x = 2
Case Is < 7.55
x = 3
Case Is < 8.04
x = 4
Case Is < 8.55
x = 5
Case Else
x = 6
End Select

'Determine row offset according to chlorine residual
'Simpler formula:
Select Case Resid
Case Is < 0.5
y = 0
Case Is >= 2.9
y = 13
Case Else
y = Int((Resid * 5) - 1.5)
End Select

dblResult = Application.WorksheetFunction.Index(rngTable, y, x)
udf_CTRequired = dblResult
End Function

I've taken the liberty of revising your test section from If...Then to Select Case.  Works faster and is less code.  I also determined the function for your chlorine residual relationship and used that instead.

Also, the Index function at the end had the x and y reversed (x is the column index and y is the row index).

I don't have your table data that is contained on the CTValues sheet, but my sample workbook appears to run correctly.

Regards,
-Glenn
0

Author Comment

ID: 40412290
I am still getting the error on this.  I am wondering that since the ranges that I am working with, basically constitute a 2d array, would it be better to read the ranges into an array and drop that part of the code altogether?
0

LVL 27

Expert Comment

ID: 40412441
Can you describe the specific error?  I tested this on a sample workbook with similarly-named sheets and a sample lookup table and had no issues.
0

Author Comment

ID: 40429417
I had the x and y reversed.  Everything is working fine.  Reading the entire post would have saved me some time.  Thanks.
0

## Featured Post

Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.