for loop VBA

JoyStrassen

New Member
Joined
Feb 19, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi, I am trying to make a for loop that hides sheets if a certain cell says No. The function is only working for one sheet and I can't work out why!

Sub testmacro()
'
For i = 4 To Sheets.Count
Worksheets(i).Activate
If WorksheetFunction.XLookup(Range("O3"), Range("B6:B19"), Range("C6:C19")) = "No" Then
Sheets(4).Visible = False
Else: Sheets(4).Visible = True
End If
Next i

End Sub

Thank you very much in advance!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the Board!

No matter what it evaluates to, you are only changing Sheet 4!
Change this:
VBA Code:
Sheets(4).Visible = False
Else: Sheets(4).Visible = True
to this:
VBA Code:
Sheets(i).Visible = False
Else: Sheets(i).Visible = True
 
Upvote 0
Solution
Welcome to the Board!

No matter what it evaluates to, you are only changing Sheet 4!
Change this:
VBA Code:
Sheets(4).Visible = False
Else: Sheets(4).Visible = True
to this:
VBA Code:
Sheets(i).Visible = False
Else: Sheets(i).Visible = True
Oh my goodness that is so obvious thank u! Clearly I had been starting at it so long
 
Upvote 0
You are welcome.

No worries, we have all been there!
:)
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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