Results 1 to 3 of 3

Thread: Implementing formulas with variables into VBA

  1. #1

    Implementing formulas with variables into VBA

    Hi I'm new to VBA.
    I'm struggling to write a script for an equation with several variables.
    Mathematically the equation is written in the form:
    X/L = AuU* ((1-e)2 (1+30(1-f)3 ) / d2 )
    I am attempting to add the variables (letters) and the overall relationship so that it can be graphed.
    Any pointers, insight or help would be appreciated.

  2. #2
    VBAX Wizard
    Apr 2007
    X/L = AuU* ((1-e)2 (1+30(1-f)3 ) / d2 )

    It's really hard to tell what are the independent variables and the independent one

    Assuming that

    1. X/L is a single number (just written wrong for VBA)
    2. AuU is a single number and not Au times U
    3. (1-e)2 means (1-e) squared
    4. (1-f)3 means (1-f) cubed
    5. d2 means d squared

    then a user defined function (UDF) in the workbook would be one way (below). The other would just be a long worksheet formula

    Option Explicit
    Function XoverL(AuU As Double, e As Double, f As Double, d As Double) As Double
        XoverL = AuU * ((1 - e) ^ 2 * (1 + 30 * (1 - f) ^ 3) / d ^ 2)
    End Function
    Sub test()
        MsgBox XoverL(100, 0.5, 12, 9)
    End Sub

    It's be a lot easier of offer suggestions if you attach a sample workbook with some dummy (but realistic) data (see my sig)

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums

  3. #3
    Moderator VBAX Wizard SamT's Avatar
    Oct 2006
    Near Columbia
    Assuming that "X" is the column you want the results in, and "A" is the column with AuU in, "E" has e in, "F" has f, and "D" has d
    'Standard Module Code.
    Option Explicit 
    Public Function XoverL(AuU As Range, e As Range, f As Range, d As Range) As Double 
        XoverL = AuU * ((1 - e) ^ 2 * (1 + 30 * (1 - f) ^ 3) / d ^ 2) 
    End Function
    In Column X, the Formula is "=XoverL(AU,E,F,D)

    Copy that formula down to the bottom of the table .Edit the letters in the Cell Formula to represent the actual Column Letters the Values are really in and place the cell formula in the appropriate Results column.
    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

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