+ Reply to Thread
Results 1 to 13 of 13

Quadratic Equation in VBA

  1. #1
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Quadratic Equation in VBA

    Hi Experts,

    I need some help to fix my Quadratic Equation in VBA. It looks fine except sqrt part.

    Here is the normal equation below X =

    -b +- √b^2-4(a)(c)
    ---------------------
    2(a)


    Here is my b value

    b = (Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1)

    Based on B my equation looks like this

    X = - Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1) - Sqr(Range("C13") * Range("C14") / Range("C14") + vt) * (xf - 1) ^ 2 - 4 * ksp / 2


    It would be great if someone can give an idea on sqrt part. Beside that all seems to be working.

    Thanks,
    Vik

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quadratic Equation in VBA

    This expression

    Range("C13") * Range("C14") / Range("C14")

    is always = C13, unless C14 = 0, in which case it blows up.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Quadratic Equation in VBA

    Quote Originally Posted by shg View Post
    This expression

    Range("C13") * Range("C14") / Range("C14")

    is always = C13, unless C14 = 0, in which case it blows up.

    Can you please explain a bit. Also advise can I use sqrt function here as I am getting error using sqrt. I have limited knowldge of VBA so would be great help for right correction of code.

    regards,
    Vik

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quadratic Equation in VBA

    a * b / b = a, as long as b <> 0, right?

    I can't help because I don't know what's on your worksheet. Maybe you could post it.

    Can you solve it with formulas?

  5. #5
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Quadratic Equation in VBA

    Based in this part of formula I am doing this as below.

    √b^2-4(a)(c)

    here b = (Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1)

    My issue here is to get sqrt. If you can give me right code to use sqrt that would be great

    so far this is the code I am using which I know it is wrong as I dont know how to use sqrt here in vba

    X = - Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1) - Sqr(Range("C13") * Range("C14") / Range("C14") + vt) * (xf - 1) ^ 2 - 4 * ksp / 2

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quadratic Equation in VBA

    A
    B
    C
    D
    E
    F
    1
    a
    b
    c
    root1
    root2
    2
    -1
    0
    0
    0.000
    0.000
    D2:E2: {=QuadRoot(A2,B2,C2)}
    3
    5
    5
    4
    #NUM!
    #NUM!
    4
    -4
    -4
    2
    -1.366
    0.366
    5
    -4
    -2
    2
    -1.000
    0.500


    Please Login or Register  to view this content.
    To enter the formula, select D2 AND E2, paste the formula in the formula bar, press and hold the Ctrl and Shift keys, then press Enter. That will make the curly braces appear; you can't enter them manually.

    Then you can drag the formula down.
    Last edited by shg; 04-09-2014 at 08:09 PM. Reason: bug :eek:

  7. #7
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Quadratic Equation in VBA

    I seen that example earlier but I have doubt as it is sqr not sqrt , Just wondering where is calculating sqrt in this above example ? square root can be done using sqr as well ?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quadratic Equation in VBA

    Sqr is the VBA square root function. Put the cursor on it and press F1.
    Last edited by shg; 04-09-2014 at 07:23 PM.

  9. #9
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Quadratic Equation in VBA

    tried that option using that example and there I am getting error 5 "Invalid procedure call or argument (Error 5)"


    result value was like -1.0000000000003e-02. I have defined all var as double , i tried to change as long but same error

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quadratic Equation in VBA

    You can't take the square root of a negative number.

  11. #11
    Registered User
    Join Date
    03-21-2014
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Quadratic Equation in VBA

    ok, i will write condition in that case

    like below , Any advise please ? here thecountXp is nothing but X only

    thecountxp1 = -(Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1)) ^ 2 - 4 * ksp
    thecountxp0 = -(Range("C13") * Range("C14") / Range("C14") + vt * (xf - 1))


    If thecountxp0 And thecountxp1 >= 0 Then
    thecountxp = (thecountxp0 + Sqr(thecountxp1)) / 2

    ElseIf thecountxp0 And thecountxp1 < 0 Then

    thecountxp = (thecountxp0 + Sqr(-thecountxp1)) / 2
    End If

  12. #12
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,918

    Re: Quadratic Equation in VBA

    Ben Van Johnson

  13. #13
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,669

    Re: Quadratic Equation in VBA

    Here is the response I posted to your other thread, which I have closed (thank you protonLeah for pointing out the duplicate). There may be other issues, as raised in the posts above, but this responds directly to post #1.

    You are on the right track. The main problem is that your parentheses are in the wrong place. But you have bigger problems than that so I'm going to make you start from scratch.

    I highly recommend that you declare variables a, b, and c. Then your formula is much more readable, and easier to set up and debug. It will look exactly like the quadratic formula, in code form.

    The quadratic formula gives two roots, but you have only calculated one of them. I am providing code to calculate both.

    Your assignment to b has a syntax error. Remove the initial left parenthesis.

    From context, it looks to me like c = ksp and a = 1. Never imply variables. If it's 1, then show a 1.

    Here is the best practice for coding this:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] how we can get quadratic equation from one graph(data series)
    By pedjvak in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-07-2012, 09:28 AM
  2. [SOLVED] Finding the Coefficients of a Quadratic Equation using VBA
    By kyleg222 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-26-2012, 10:38 AM
  3. How to determine factor A, B, C of quadratic equation
    By utanvn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-25-2012, 07:29 AM
  4. [SOLVED] solve quadratic equation system
    By Miguel in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-27-2005, 10:06 AM
  5. Quadratic equation
    By Nes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-11-2005, 10:52 PM

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