Results 1 to 7 of 7

VBA If else statement

Threaded View

peedarp1985 VBA If else statement 05-11-2010, 02:22 PM
royUK Re: VBA programming help 05-11-2010, 02:22 PM
foxguy Re: VBA If else statement 05-11-2010, 06:44 PM
peedarp1985 Re: VBA If else statement 05-11-2010, 11:33 PM
foxguy Re: VBA If else statement 05-14-2010, 05:58 PM
Leith Ross Re: VBA If else statement 05-14-2010, 09:28 PM
peedarp1985 Re: VBA If else statement 05-14-2010, 11:52 PM
  1. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA If else statement

    Hello peedarp1985,

    Your If Then statements are not working because the data being evaluated is a String and not a Number. All data going into and coming out of a TextBox is a String. You will need to convert the text to a number before you test it. It is easier to read using the Select Case statement rather than multiple If Then. Indent your code. It makes it easier to read and to correct.
    Private Sub Tolerance_Click()
    
    Dim Msg1 As String
    Dim D As Double
    Dim L As Double
    Dim Ts As Double
    Dim S As Double
    Dim Ci As Double
    Dim Cy As Double
    
    Msg1 = Msg1 & ComboBox1
    
    D = D + Val(TextBox1)
    L = L + Val(TextBox2)
    
    Select Case  Msg1
      Case is = "Size"
        Select Case D
          Case Is >= 0.25
            Ts = 0.001 + (0.006 + 0.005 * D)
          Case Is < 0.25
            Ts = 0.001 + (0.003 + 0.016 * D)
        End Select
        MsgBox Ts
      Case Is = "Straightness"
        S = L * 0.0087
        MsgBox S
      Case Is = "Circularity"
        Ci = 0.25 * Size
        MsgBox Ci
      Case Is = "Cylindricity"
        Cy = S + Ci
        MsgBox Cy
    End Select
    
    End Sub
    Last edited by Leith Ross; 05-14-2010 at 09:35 PM. Reason: Changed Integers to Doubles in Dim statements
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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