End If Without Block If issue

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi
I seem to have the correct number of 'End If''s in the below code, but I'm still getting the 'End If Without Block If' error message.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim b As Integer
b = Range("E5").Value Mod 100 'every 100 sessions

If b = 100 - 1 And Range("F3") = "" Then MsgBox "The next session will be your " & Format(Range("E5").Value + 1, "#,##0") & "th session on the bike", vbInformation, "Approaching " & Format(Range("E5").Value + 1, "#,##0") & " Bike Sessions"
Range("F3") = "1"
End If
If b = 0 Then MsgBox "Congratulations! You have just completed your " & Format(Range("E5").Value - b, "#,##0") & "th session!", vbInformation, "Bike Sessions Completed"
Range("F3") = "1"
End If
Application.ScreenUpdating = True
End Sub
Help would be appreciated.

Many thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Resolved.
VBA Code:
If b = 100 - 1 And Range("F3") = "" Then
MsgBox "The next session will be your " & Format(Range("E5").Value + 1, "#,##0") & "th session on the bike", vbInformation, "Approaching " & Format(Range("E5").Value + 1, "#,##0") & " Bike Sessions"
Range("F3") = "1"
If b = 0 Then
MsgBox "Congratulations! You have just completed your " & Format(Range("E5").Value - b, "#,##0") & "th session!", vbInformation, "Bike Sessions Completed"
Range("F3") = "1"
End If
End If
Application.ScreenUpdating = True
End Sub
I've no idea why putting "Msgbox..." on a new line eliminated the error (I had tried rearranging the order of the End If's and that didn't make any difference)
 
Last edited:
Upvote 0
Many thanks bebo - my amendment seemed to work even though the 'end if's were placed together. Does this matter?

Edit - it does matter. When I amended the position to match yours I ended up in an endless loop of message boxes.
 
Upvote 0
When you had the Msgbox's on a single line then you didn't need any End If's, which is why you got the error.
Probably wouldn't have gave the answer that you wanted as your solution post nests one If statement within the other.
 
Upvote 0
Thanks Mark!

I have a similar problem with the below, with an 'else without if' error
VBA Code:
Dim d As Long
d = [F5] Mod 650

If d > 171 And d < 186 and Range ("H4") = "" Then
MsgBox "You've almost reached 650 miles in your running shoes" & vbNewLine & vbNewLine _
& "Nearly time to buy a new pair!", vbInformation, "Running Shoes Nearly Worn Out"
Range("H4") = "1"

ElseIf d = 186 and Range ("H4") = "" Then
MsgBox "You've now reached 650 miles in your running shoes" & vbNewLine & vbNewLine _
& "Time to buy a new pair!", vbInformation, "Running Shoes Worn Out"
Range("H4") = "1"

ElseIf d > 186 And d < 216 and Range ("H4") = "" Then
MsgBox "You've exceeded 650 miles in your running shoes" & vbNewLine & vbNewLine _
& "You need to buy a new pair!", vbInformation, "Running Shoes Worn Out"
Range("H4") = "1"

Application.EnableEvents = True
End Sub
 
Upvote 0
That one does need an End If above the Application.EnableEvents = True (If you Indent your code it shows up easier?)
 
Upvote 0
Solution
That's brilliant, thanks ever so much Mark! (I still use the formula you very kindly gave me in 2015
=SUMPRODUCT(--('Daily Tracking'!$A$2:$A$367>=DATE(YEAR(TODAY())-17,1,1))*(--('Daily Tracking'!$A$2:$A$367<DATE(YEAR(TODAY())-17,MONTH(TODAY()),DAY(TODAY())))),('Daily Tracking'!AH$2:AH$367)) ) ;)
 
Upvote 0
You're welcome, happy to be of help again
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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