VBA Issue

Mattandy55

New Member
Joined
May 27, 2016
Messages
20
I have a code that should be basic enough.. But for some reason when running it doesn't work correctly for Idaho and Colorado. It hides Colorado and Idaho even though B2 on each sheet aren't 0. Job Data, Arizona, and Utah all work perfectly. Any assistance is greatly appreciated.



VBA Code:
Sub HideSheets()

Application.DisplayAlerts = False
    Sheets("Job Data").Select
    If [A3] = 0 Then
Sheets("Job data").Visible = False
Else
Sheets("Job Data").Visible = True
End If
    Sheets("Arizona").Select
    If [B2] = 0 Then
Sheets("Arizona").Visible = False
Else
Sheets("Arizona").Visible = True
End If
Sheets("Utah").Select
    If [B2] = 0 Then
Sheets("Utah").Visible = False
Else
Sheets("Utah").Visible = True
End If
Sheets("Colorado").Select
    If [B2] = 0 Then
Sheets("Colorado").Visible = False
Else
Sheets("Colorado").Visible = True
End If
Sheets("Idaho").Select
    If [B2] = 0 Then
Sheets("Idaho").Visible = False
Else
Sheets("Idaho").Visible = True
End If



Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:
Range is definitely a property of a worksheet
Yes Range is, but rng is not - it's a variable that you declared, so you can't use Sheets("some sheet").rng. Nor do you need to since rng already refers to a specific existing range on one specific sheet.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Well, until I smarten up I will believe that rng is a range object since I declared it as such.
Dim rng As Range
so rng is a property of a sheet. I still think the issue was the double reference to the sheet.
 
Upvote 0
I still think the issue was the double reference to the sheet.
No. The issue is that you have declared rng as a range object and set it to cell B2 on a particular sheet - the active sheet when the code is run.


Easy to demonstrate
Make all sheets visible and make Arizona the active one. Now step through your code. It will process (hide or not hide) 'Job data' since that has nothing to do with rng. It will process (hide or not hide) 'Arizona' since rng is cell B2 on that sheet. It will not process 'Utah' because rng is set to B2 on 'Arizona', not Utah'

Make all sheets visible again and make 'Utah' the active sheet then step through the code. This time it will fail on 'Arizona' because rng will have been set to B2on 'Utah'
 
Upvote 0
Well, until I smarten up I will believe that rng is a range object since I declared it as such.
Dim rng As Range

Yes, that is correct.

so rng is a property of a sheet.

No, rng is a variable that you created. It's also an object not a property.

If you write:

Code:
Dim ws as worksheet
Dim rng as Range
set ws = sheets("Sheet1")
set rng = ws.Range("A1")

when you type the ws. in the last line, Intellisense will pop up a list of properties and methods of a worksheet. rng is not one of them, whereas Range is.
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,703
Members
452,667
Latest member
vanessavalentino83

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