Error on hide rows conditionally

aliaslamy2k

Active Member
Joined
Sep 15, 2009
Messages
416
Office Version
  1. 2019
Platform
  1. Windows
Dear All ,

I am using below VBA code to hide rows if criteria is met, but i am getting " Complie Error; Next without For "



Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B5:B10")
If c = "Jack" Or c = "Molly" Then _
Rows(c.Row).EntireRow.Hidden = True
Next
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
That works OK for me. Perhaps make it more explicit

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("B5:B10")
    If c.Value = "Jack" Or c.Value = "Molly" Then Rows(c.Row).EntireRow.Hidden = True
Next c
End Sub
 
Upvote 0
You need an 'End If' just before the 'Next'
 
Upvote 0
Hi Vog,

Still I am not getting, when I enter criteria, it takes me back to VBA code and highlights " End Sub" in Yellow. I even tried entering End If Just before NEXT, but no result. Getting an error!

Note : I am using Excel 2007



Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("B5:B10")
If c.Value = "Jack" Or c.Value = "Molly" Then Rows(c.Row).EntireRow.Hidden = True
Next c
End Sub
 
Upvote 0
Well, it works for me in Excel 2010.

Do you have any other code in that sheet's code module?
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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