Paul KIm
Paul KIm

Reputation: 3

Syntax error with simple MsgBox

enter image description here

I am just learning VBA program on my own at work.

I have attached a picture of the syntax error.

I keep getting an error at the msgbox.

What did I do wrong?

Private Sub CommandButton1_Click()
Dim firstnum, secondnum As Single
firstnum = Cells(1, 1).Value
secondnum = Cells(1, 2).Value
If firstnum > secondnum Then
MsgBox " The first number is greater than the second number"
If firstnum < secondnum Then
MsgBox " The first number is less than the second number"
Else
MsgBox " They are euqal "
End If

End Sub

Upvotes: 0

Views: 633

Answers (1)

Moreno
Moreno

Reputation: 638

I made some changes in order to check you program. The first thing i noticed is that you are using two if statements and only closing the first one, i mean for every if you need an end if statement. Second, i think two if are unnecessary since you can use elseif statement

Private Sub test()
Dim firstnum, secondnum As Single
 firstnum = Cells(1, 1).Value
 secondnum = Cells(1, 2).Value
If firstnum > secondnum Then
 MsgBox " The first number is greater than the second number"
ElseIf firstnum < secondnum Then
 MsgBox " The first number is less than the second number"
Else
 MsgBox " They are euqal "
End If
End Sub

Finally as an advice try always using indentation.

Upvotes: 1

Related Questions