Nested If....Then...Else statement

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
Hi,

I'm getting an error (else without an if) on the following if then else statement:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, Cancel As Boolean)


Dim Ans As Long


If target.Value = "" Then
    If target.Offset(0, -3) = "" Then
        usf_Main.Show
        Else: Ans = MsgBox("Add new row?", vbYesNo)
    End If
    If Ans = vbNo Then Exit Sub
        Else: target.EntireRow.Insert , copyorigin:=xlFormatFromRightOrAbove
    End If
    Else: usf_Adjust.Show
End If


End Sub

First it tests if the target cell is blank, if it is, then it tests if the cell three columns to the left is blank, if it is then a userform is opened. If the second tested cell isn't blank, then a msgbox opens asking to insert a new row.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Solved....I had:

Code:
[COLOR=#333333]If Ans = vbNo Then Exit Sub[/COLOR]

All as one line...it should have been:

Code:
[COLOR=#333333]If Ans = vbNo Then 
Exit Sub[/COLOR]
 
Upvote 0
Or you could write (more simply, IMHO):

Rich (BB code):
If target.Value = "" Then
    If target.Offset(0, -3) = "" Then
        usf_Main.Show
    Else
        Ans = MsgBox("Add new row?", vbYesNo)
    End If
    If Ans = vbNo Then Exit Sub
    target.EntireRow.Insert , copyorigin:=xlFormatFromRightOrAbove
Else
    usf_Adjust.Show
End If

Since Exit Sub is essentially a "go to", there is no need for an Else part.

Also notice how Else lines up with its matching If and End If. And the Else body starts on a new line and it is indented.

IMHO, that makes the code structure clearer.

FYI, you could also simplify the code structure by using the "one-line" If-Then-Else construct -- although, in this case, I would use the continuation "operator" to put Else on a separate line.

Rich (BB code):
If target.Value = "" Then
    If target.Offset(0, -3) = "" Then usf_Main.Show _
    Else Ans = MsgBox("Add new row?", vbYesNo)
    If Ans = vbNo Then Exit Sub
    target.EntireRow.Insert , copyorigin:=xlFormatFromRightOrAbove
Else
    usf_Adjust.Show
End If

The second If statement is essentially:

If target.Offset(0, -3) = "" Then usf_Main.Show Else Ans = MsgBox("Add new row?", vbYesNo)

with a "line break" (underscore continuation operator).
 
Upvote 0
Or you could write (more simply, IMHO):

Rich (BB code):
If target.Value = "" Then
    If target.Offset(0, -3) = "" Then
        usf_Main.Show
    Else
        Ans = MsgBox("Add new row?", vbYesNo)
    End If
    If Ans = vbNo Then Exit Sub
    target.EntireRow.Insert , copyorigin:=xlFormatFromRightOrAbove
Else
    usf_Adjust.Show
End If

Since Exit Sub is essentially a "go to", there is no need for an Else part.

Also notice how Else lines up with its matching If and End If. And the Else body starts on a new line and it is indented.

IMHO, that makes the code structure clearer.

FYI, you could also simplify the code structure by using the "one-line" If-Then-Else construct -- although, in this case, I would use the continuation "operator" to put Else on a separate line.

Rich (BB code):
If target.Value = "" Then
    If target.Offset(0, -3) = "" Then usf_Main.Show _
    Else Ans = MsgBox("Add new row?", vbYesNo)
    If Ans = vbNo Then Exit Sub
    target.EntireRow.Insert , copyorigin:=xlFormatFromRightOrAbove
Else
    usf_Adjust.Show
End If

The second If statement is essentially:

If target.Offset(0, -3) = "" Then usf_Main.Show Else Ans = MsgBox("Add new row?", vbYesNo)

with a "line break" (underscore continuation operator).

Thanks! I always appreciate the extra explanations and suggestions!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top