Method Range of Object Worksheet failing

mdd16

Board Regular
Joined
Jan 11, 2011
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hello.. can someone please help me debug this supposedly simple code.. I am getting error Method Range of Object Worksheet failing

I am trying to hide those sheets where Range ("DCT_Status").Value = "Complete"
Each sheet has a range named "DCT_Status" which is mapped to the sheet.

VBA Code:
 For Each ws In ThisWorkbook.Worksheets
   If ws.Visible = xlSheetVisible And ws.Range("DCT_Status").Value = "Complete" Then
        ws.Visible = xlSheetHidden
   End If
   Next ws
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi. Try this code, please.

VBA Code:
Sub HideUnhideSheets()
 Dim ws As Worksheet
  For Each ws In ThisWorkbook.Worksheets
   On Error Resume Next 'to avoid error if some sheet doesn't have that named range
   ws.Visible = ws.Range("DCT_Status").Value <> "Complete"
   On Error GoTo 0
  Next ws
End Sub
 
Upvote 0
I believe that if you are doing this from another sheet, or a standard module, you have to preface the worksheet range name with the sheet name. Maybe the reference is like Range("'" & ws.Name & "!DCT_Status")
I can get a reference to a range using that syntax but since I don't have the same range name on every sheet I can't fully test it.
 
Upvote 0
or a standard module, you have to preface the worksheet range name with the sheet name
Doesn't need it for me
VBA Code:
ws.Range("DCT_Status").Value
works fine with just the ws variable
 
Last edited:
Upvote 0
Solution
I didn't test it, so that's why I said "I believe".
 
Upvote 0
Thanks Micron, Thanks Mark.. error was on my side. I had spelling error on the range name and that of course caused the trouble. Thanks for helping out
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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