Solved

Unable to get the Index Property of the WorksheetFunction Class

Posted on 2014-10-23
4
306 Views
Last Modified: 2014-11-07
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
Comment
Question by:AaronGreene1906
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
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

Open in new window


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

by:AaronGreene1906
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

by:Glenn Ray
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

by:AaronGreene1906
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question