Conditional Hide issue

ashrayp

New Member
Joined
Aug 13, 2019
Messages
1
[FONT=&quot]I have the follow script in vba:[/FONT]
[FONT=&quot]Sub ConditionalHide()
Dim cRange As String
cRange = Sheets(1).Range("H3")
For i = 2 To Sheets.Count
Sheets(i).Activate
If Range("H3").Value <> cRange Then
Sheets(i).Visible = False
Else: Sheets(i).Visible = True
End If
Next i
End Sub

The problem is that it gives me Expected error on Line 6. I am new to VBA and cannot make out whats wrong with my code. I am trying to Hide all the sheets that do not match the value in "H3" in Sheet 1.
[/FONT]
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the Board!

If you have Option Explicit turned on, then you will need to declare the i variable also, i.e.
Code:
Sub ConditionalHide()

    Dim cRange As String
    Dim i As Long

    cRange = Sheets(1).Range("H3")

    For i = 2 To Sheets.Count
        Sheets(i).Activate
        If Range("H3").Value <> cRange Then
            Sheets(i).Visible = False
        Else: Sheets(i).Visible = True
        End If
    Next i
    
End Sub
Other than that, the code looks fine to me and worked when I tested it.
Are you sure that you don't have any errors in cell H3 on any sheet?
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

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