End if without Block if followed by Block if without End if !

nmounir

Board Regular
Joined
Oct 16, 2020
Messages
107
Office Version
  1. 365
Platform
  1. Windows
Good day,

I am trying to write this code but i keep getting this error all the time.

any idea how to fix this?

Thank you
 

Attachments

  • end if without block if.PNG
    end if without block if.PNG
    21.7 KB · Views: 41
  • End if without block if 2.PNG
    End if without block if 2.PNG
    21.4 KB · Views: 42
The thing to remember when working with multi-line blocks in VBA (like "If/End If", "With/End If", "For/Next"), the LAST one started must be the FIRST one to close (and likewise, the FIRST one started must be the LAST one to close).

If you indent your code properly, it make it very easy to see and follow along, i.e.

Rich (BB code):
With Range(...)
    For ...
        If ...

        End If
    Next
End With
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
i tried to put the end if before and after and both methods didn't work !!!
Post the code when you tried what I suggested & show the error you get.
 
Upvote 0
The thing to remember when working with multi-line blocks in VBA (like "If/End If", "With/End If", "For/Next"), the LAST one started must be the FIRST one to close (and likewise, the FIRST one started must be the LAST one to close).

If you indent your code properly, it make it very easy to see and follow along, i.e.

Rich (BB code):
With Range(...)
    For ...
        If ...

        End If
    Next
End With
I am a VBA Beginner so i may not able to ident it correctly

Dim MyRenewedCDs As Range
Dim CD As Range
Application.EnableEvents = False

Set MyRenewedCDs = Range("J47:J71")
If Not Intersect(Target, MyRenewedCDs) Is Nothing Then
For Each CD In MyRenewedCDs
If CD.Value = "" Then CD.Offset(0, 3).Value = 0

Next CD
End If

Application.EnableEvents = True

End Sub
 
Upvote 0
Post the code when you tried what I suggested & show the error you get.
here is the code with the way you suggested. i didn't get any error but the code didn't fire up. as you can see the 2% remains while it should have replaced by Zero

Jul-2022​
Renewal of Exp CD'sNew Purchase DateInterest RateInterest Amount
$3,000​
4-Jun-22
2%​
$5​
2%​
2%​
2%​

Dim MyRenewedCDs As Range
Dim CD As Range
Application.EnableEvents = False

Set MyRenewedCDs = Range("J47:J71")
If Not Intersect(Target, MyRenewedCDs) Is Nothing Then
For Each CD In MyRenewedCDs
If CD.Value = "" Then CD.Offset(0, 3).Value = 0

Next CD
End If

Application.EnableEvents = True

End Sub
 
Upvote 0
I am a VBA Beginner so i may not able to ident it correctly
Forget the indenting for a moment (that just makes it easier to visualize).
Note what I said in my previous post, and take a look at the pattern (I color-coded it to make it obvious).
If you understand that, then it will become evident exactly what you need to do to fix your code.
 
Upvote 0
Put this code in a normal module & run it. Then with the code you posted in post#15 try changing a value in J47:J71
 
Upvote 0
Put this code in a normal module & run it. Then with the code you posted in post#15 try changing a value in J47:J71
I am getting the same error in a normal module.
 

Attachments

  • Table 5.PNG
    Table 5.PNG
    20.7 KB · Views: 2
Upvote 0
Keep the code exactly as you have it in post#15.
 
Upvote 0
Forget the indenting for a moment (that just makes it easier to visualize).
Note what I said in my previous post, and take a look at the pattern (I color-coded it to make it obvious).
If you understand that, then it will become evident exactly what you need to do to fix your code.
Sub Changeevent()

Dim MyRenewedCDs As Range
Dim CD As Range
Application.EnableEvents = False

Set MyRenewedCDs = Range("J47:J71")
' If Not Intersect(Target, MyRenewedCDs) Is Nothing Then
For Each CD In MyRenewedCDs
If CD.Value = "" Then CD.Offset(0, 3).Value = 0

Next CD
End If

Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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