I have a code where if some buttons are selected its showing in a message box which was selected, but if no button was selected I want to show a message in the message box.
Here is the code, please make me understand, I tried by my own but its showing the error:
Else without If, or Expected End Of Statement:

Private Sub cmdBuild_Click()
Dim Message As String
Dim I As Integer
Message = PizzaWhere + vbCr
Message = Message + PizzaSize + " Pizza" + vbCr
Message = Message + PizzaCrust + vbCr
For I = 0 To 5
If chkTop(I).Value = vbChecked Then Message = Message _
+ chkTop(I).Caption + vbCr
Else
If chkTop(I).Value = vbUnchecked Then Message = Message _
+ "Cheese Only" + vbCr
Next I
MsgBox Message, vbOKOnly, "Your Pizza"
End If
End If
End Sub

If you properly indent your code then it looks like

Private Sub cmdBuild_Click()

    Dim Message As String
    Dim I As Integer

    Message = PizzaWhere + vbCr
    Message = Message + PizzaSize + " Pizza" + vbCr
    Message = Message + PizzaCrust + vbCr

    For I = 0 To 5
        If chkTop(I).Value = vbChecked Then
            Message = Message + chkTop(I).Caption + vbCr
        Else 
            If chkTop(I).Value = vbUnchecked Then 
                Message = Message + "Cheese Only" + vbCr
    Next I

    MsgBox Message, vbOKOnly, "Your Pizza"

    End If
    End If

End Sub

Once you do that the problem becomes a little more obvious. That was my best guess as to the formatting. If I just copy your code into the IDE I get

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Message As String
        Dim I As Integer
        Message = PizzaWhere + vbCr
        Message = Message + PizzaSize + " Pizza" + vbCr
        Message = Message + PizzaCrust + vbCr
        For I = 0 To 5
            If chkTop(I).Value = vbChecked Then Message = Message _
            + chkTop(I).Caption + vbCr
Else
            If chkTop(I).Value = vbUnchecked Then Message = Message _
            + "Cheese Only" + vbCr
        Next I
        MsgBox(Message, vbOKOnly, "Your Pizza")
End If
End If
    End Sub

Perhaps if you explain a little more clearly I can suggest something.

Please make full use of code button when are you going to put code here.right ?

now let's come on the thread.
Errors occur because you are using Inline If and when you are using Inline If then there is no need to put End If

and more issue is that if you decide not to use Inline If then you have to put End If before Next Not After Next because all these Ifs are put inside For_Next so first use End If then use Next.

now your code will look like this :

Private Sub cmdBuild_Click()
    Dim Message As String
    Dim I As Integer
    Message = PizzaWhere + vbCr
    Message = Message + PizzaSize + " Pizza" + vbCr
    Message = Message + PizzaCrust + vbCr
    For I = 0 To 5
        If chkTop(I).Value = vbChecked Then
            Message = Message + chkTop(I).Caption + vbCr
        Else If chkTop(I).Value = vbUnchecked Then
            Message = Message + "Cheese Only" + vbCr
            MsgBox Message, vbOKOnly, "Your Pizza"
        End If
    Next I
End Sub

hope this helps you.

Edited 3 Years Ago by Reverend Jim: fixed formatting

This question has already been answered. Start a new discussion instead.