Consulting

Results 1 to 2 of 2

Thread: “if - elseif - then - else” statement is not working

  1. #1
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    2
    Location

    “if - elseif - then - else” statement is not working

    Hi

    I have three texboxes, called tbttUg1,tbttUg2 and tbttUg3. The basic is, that when i click on a "Add" button, the first textbox shows a result of an equation. When I click the second time on it, the second textbox shows a result.

    the code is:

    Private Sub CommandButton1_Click()
    Dim bb As Integer
    Dim dt As Integer

    bb = tbttA.Value / (0.5 * tbttP.Value)
    dt = tbttw.Value + cbttL.Value * (0.17 + 1 / tbttU.Value)
    mif = 2 * cbttL.Value / (3.1415 * bb + dt) * Application.WorksheetFunction.Ln(3.1415 * bb / dt + 1)
    wif = cbttL.Value / (0.457 * bb + dt)

    If tbttUg1.Value = "" And dt < bb Then
    tbttUg1.Value = Round(mif, 4)
    ElseIf tbttUg1.Value = "" And dt >= bb Then
    tbttUg1.Value = Round(wif, 4)
    ElseIf tbttUg1.Value > 0 And dt < bb Then
    tbttUg2.Value = Round(mif, 4)
    ElseIf tbttUg1.Value > 0 And dt >= bb Then
    tbttUg2.Value = Round(wif, 4)
    ElseIf tbttUg2.Value > 0 And dt < bb Then
    tbttUg3.Value = Round(mif, 4)
    ElseIf tbttUg2.Value > 0 And dt >= bb Then
    tbttUg3.Value = Round(wif, 4)
    Else
    MsgBox "halo"
    End If

    End Sub

    The problems it, that adding works only for two texboxes, I don't know why, but the third adding always re-writes the second one.
    Does anyone sees the mistake in coding?
    Furthermore, is there any better way for adding ans seeing an interim result on a single userform page?
    Thanks for any kind of feedback!

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    I dont seet wif as a DECLARED VARIABLE

    also CASE statements are easier to read..

    select case true
    case  tbttUg1.Value = "" And dt < bb 
        tbttUg1.Value = Round(mif, 4)
    case  tbttUg1.Value = "" And dt >= bb 
     tbttUg1.Value = Round(wif, 4)
    case  tbttUg1.Value > 0 And dt < bb 
     tbttUg2.Value = Round(mif, 4)
    case  tbttUg1.Value > 0 And dt >= bb 
     tbttUg2.Value = Round(wif, 4)
    case  tbttUg2.Value > 0 And dt < bb 
     tbttUg3.Value = Round(mif, 4)
    case  tbttUg2.Value > 0 And dt >= bb 
     tbttUg3.Value = Round(wif, 4)
    case Else
       MsgBox "halo"
    End select

Posting Permissions

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