Consulting

Results 1 to 6 of 6

Thread: How to fix Compile error: Else without if

  1. #1

    Post How to fix Compile error: Else without if

    hi! can someone help me. i am practicing to create a calculator using Microsoft Visual Basic and it's giving me a error: (Compile error: Else without if)
    the bold one is the error line
    heres the code:

    Dim value1 As Double
    Dim value2 As Double
    Dim op As String
    
    
    Private Sub Command1_Click()
    Text1.Text = Text1.Text + "7"
    End Sub
    
    
    Private Sub Command11_Click()
    Text1.Text = Text1.Text + "0"
    End Sub
    
    
    Private Sub Command12_Click()
    If op = "+" Then Text1.Text = value1 + Text1.Text
    ElseIf op = "-" Then Text1.Text = value1 + Text1.Text 
    ElseIf op = "*" Then Text1.Text = value1 + Text1.Text
    ElseIf op = "/" Then Text1.Text -value1 = Text1.Text
    Else: Text1.Text = ""
    End If
    End Sub
    
    
    Private Sub Command13_Click()
    op = "+"
    value1 = Text1.Text
    Text1.Text = ""
    
    
    End Sub
    
    
    Private Sub Command14_Click()
    op = "-"
    Text1.Text = value1 + Text1.Text
    Text1.Text = ""
    End Sub
    
    
    Private Sub Command2_Click()
    Text1.Text = Text1.Text + "8"
    End Sub
    
    
    Private Sub Command3_Click()
    Text1.Text = Text1.Text + "9"
    End Sub
    
    
    Private Sub Command4_Click()
    Text1.Text = Text1.Text + "4"
    End Sub
    
    
    Private Sub Command5_Click()
    Text1.Text = Text1.Text + "5"
    End Sub
    
    
    Private Sub Command6_Click()
    Text1.Text = Text1.Text + "6"
    End Sub
    
    
    Private Sub Command7_Click()
    Text1.Text = Text1.Text + "1"
    End Sub
    
    
    Private Sub Command8_Click()
    Text1.Text = Text1.Text + "2"
    End Sub
    
    
    Private Sub Command9_Click()
    Text1.Text = Text1.Text + "3"
    End Sub

  2. #2
    It should be like
    Private Sub Command12_Click()
    If Op = "+" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "-" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "*" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "/" Then
       Text1.Text -Value1 = Text1.Text
    Else: Text1.Text = ""
    End If
    End Sub

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If op = "+" Then Text1.Text = value1 + Text1.Text

    IS a one line If and there is an assumed End If after it

    If op = "+" Then Text1.Text = value1 + Text1.Text [End If]
    ElseIf [Else without If]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4

    Thankyou sir! it is now working

    Thanks! it is now working perfectly.

    Quote Originally Posted by Fluff View Post
    It should be like
    Private Sub Command12_Click()
    If Op = "+" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "-" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "*" Then
       Text1.Text = Value1 + Text1.Text
    ElseIf Op = "/" Then
       Text1.Text -Value1 = Text1.Text
    Else: Text1.Text = ""
    End If
    End Sub

  5. #5
    Thanks

  6. #6
    Glad to help & thanks for the feedback

Posting Permissions

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