To get a feeling for how If ... End If works, try this example:
Code: Select all
Sub Test1()
Dim n As Integer
n = InputBox("Enter a whole number")
If n > 10 Then
MsgBox "You should see this only if you entered a number greater than 10."
End If
MsgBox "You should see this whatever you entered."
End Sub
Copy the code into a standard module in the Visual Basic Editor in Word or Excel.
Run this macro, and enter a number below 10, for example 5. You should see one message box.
Run the macro again and enter a number over 10, for example 13. You should see two message boxes, one after another.
Next, try this:
Code: Select all
Sub Test2()
Dim n As Integer
n = InputBox("Enter a whole number")
If n > 10 Then
MsgBox "You should see this only if you entered a number greater than 10."
Else
MsgBox "You should see this only if you entered a number less than or equal to 10."
End If
MsgBox "You should see this whatever you entered."
End Sub
When you run the code, you should see two message boxes whether you enter a number under 10 or over 10, but the first message box will be different depending on what you entered.