Solved

Unable to get the Index Property of the WorksheetFunction Class

Posted on 2014-10-23
4
274 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

937 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now