Results 1 to 3 of 3

Passing Spreadsheet Ranges to VBA LINEST function

Threaded View

TechMann Passing Spreadsheet Ranges to... 06-21-2011, 03:53 PM
protonLeah Re: Passing Spreadsheet... 06-21-2011, 10:21 PM
TechMann Re: Passing Spreadsheet... 06-22-2011, 11:09 AM
  1. #1
    Registered User
    Join Date
    03-09-2011
    Location
    Birmingham, Alabama USA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Passing Spreadsheet Ranges to VBA LINEST function

    I am writing a VBA function which will take X and Y curve points and determine their polynomial trendline coefficients. I don't want to create an Excel chart to obtain the coefficients.

    Eventually I'd like to let the user select a set of X and Y points on a spreadsheet, pass them to the VBA function and return different values back to the spreadsheet.

    J-Walk & Associates webpage provides equations for calculating the trendline coefficients. Unforturnately, I can't get the function to work.

    Here is the primary code which is causing the problem.
    Sub TestHead()
        Dim Answer As Variant
        Answer = PumpHead(Worksheets("Sheet1").Range("A2:A8"), Worksheets("Sheet1").Range("B2:B8"), 800, 6)
    End Sub
    
    Public Function PumpHead(xFlow As Range, yTDH As Range, Flow As Double, Order As Integer) As Variant
        Dim xFlowUB As Integer, yTDHUB As Integer
        Dim i As Integer
        Dim Head1 As Double, Test As Double
        Dim coeff()
        
    'Check Inputs
    'Here I have several error checking routines to ensure the values passed to the function 
    'are acceptable.
        
    'Begin coeff and PumpHead calculation
        Select Case Order
            Case 2
                coeff = Evaluate("LINEST(yTDH,xFlow^{1,2})")
                PumpHead = coeff(1) * Flow ^ 2 + coeff(2) * Flow + coeff(3)
            Case 3
                coeff = Evaluate("LINEST(yTDH,xFlow^{1,2,3})")
                PumpHead = coeff(1) * Flow ^ 3 + coeff(2) * Flow ^ 2 + coeff(3) * Flow + coeff(4)
            Case 4
                coeff = Evaluate("LINEST(yTDH,xFlow^{1,2,3,4})")
                PumpHead = coeff(1) * Flow ^ 4 + coeff(2) * Flow ^ 3 + coeff(3) * Flow ^ 2 + coeff(4) * Flow + coeff(5)
             Case Else
                PumpHead = "ERROR!#"
        End Select
    
    ExitFunction:
    End Function
    When I run the code above I get the error "Type mismatch". I tried converting the xFlow and yTDH values to an array but with no success.

    Thanks in advance.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1