Results 1 to 7 of 7

Pythagoras in a keypress form

Threaded View

  1. #1
    Registered User
    Join Date
    11-04-2006
    Posts
    19

    Pythagoras in a keypress form

    Hi. I am trying to create a form that calculates Pythagoras's Theorem, the rest of this post gets a bit complicated so I have tried to reduce the problem, to make it easier to work out. You could ignore this post and go on to the next post just below...

    a squared + b squared = c squared
    a^2 + b^2 = c^2.

    \1

    where c is the hypotenuse of a right-angled triangle (the longest side, which is directly opposite the right-angle)

    and a and b are the other 2 sides (known as the adjacent and the opposite)

    I have created a userform with 3 main TextBoxes known as: Adjacent, Opposite and Hypotenuse.

    I want the form to work so that if someone types in values for the opposite and the adjacent sides e.g. 3 and 4, then the form will automatically calculate the hypotenuse, (in this case 5), without anyone having to click any buttons.

    I can do this with this code:

    Sub Calculate_Hypotenuse()

    If Opposite = "" Then Opposite = 0
    If Adjacent = "" Then Adjacent = 0
    OppositeSquared = Opposite * Opposite
    AdjacentSquared = Adjacent * Adjacent

    HypotenuseSquared = OppositeSquared * 1 + AdjacentSquared * 1 'I had to put * 1 in here or else it goes doo-lally

    Range("A1") = HypotenuseSquared
    Range("A2").FormulaR1C1 = "=Sqrt(R[-1]C)" 'Square root the HypotenuseSquared variable to get the Hypotenuse.
    Hypotenuse = Range("A2")

    End Sub
    Private Sub Opposite_Change()

    If Opposite = "" Then Opposite = 0
    Call Calculate_Hypotenuse

    End Sub
    Private Sub Adjacent_Change()

    If Adjacent = "" Then Adjacent = 0
    Call Calculate_Hypotenuse

    End Sub

    However, being greedy, I also want the form to automatically count the opposite side, when someone types in the adjacent and the hypotenuse. This is where it started going wrong. I tried adding this code but the computer hates it, and almost spat at me.

    Private Sub Hypotenuse_Change()

    If Hypotenuse = "" Then Hypotenuse = 0

    HypotenuseSquared = Hypotenuse * Hypotenuse
    AdjacentSquared = HypotenuseSquared - OppositeSquared

    'If the HypotenuseSquared is less than the OppositeSquared then the AdjacentSquared will be a negative number, which is bad news for square roots so this next line stops that.
    If AdjacentSquared < 0 Then AdjacentSquared = AdjacentSqaured * -1

    Range("A2") = AdjacentSquared
    Range("A1").FormulaR1C1 = "=Sqrt(R[-1]C)"
    Opposite = Range("A1")

    End Sub

    I don't what I am doing wrong. Please help!
    Last edited by heinousanus; 12-09-2006 at 01:54 AM.

Thread Information

Users Browsing this Thread

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

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