You are here:

Excel/Hide Worksheet Based on Cell Value

Advertisement


Question
Hi Tom,

I am hoping that you can help me with this macro.

I am trying to hide worksheets within a workbook if the last cell value in column "O" is less than 500. After I run the macro, certain worksheets got hidden, but it stops hiding after certain sheets. For example:

Sheet1: cell value is 0. The sheet got hidden, which is working as expected.

Sheet2: cell value is 230. The sheet got hidden, which is working as expected.

Sheet3: cell value is 589. The sheet is visible, which is working as expected.

Sheet4: cell value is 345. The sheet did not get hidden, which is not working as expected.

It seems that it stops hiding sheets starting from Sheet4.

I am not sure if I missed anything in the code... This is what I have:

Sub HideSheetBasedOnCellValue()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
 
  If Cells(ActiveSheet.Cells(ActiveSheet.Rows.Count, "O").End(xlUp).Row, "O").Value < 500 Then
     ws.Visible = False
  Else: ws.Visible = True
  
  End If
   
Next ws

End Sub

Thanks in advance for your help,

Tim

Answer
Tim

You make your decision on the the value of the last cell in column O in the activesheet.  As soon as a sheet is not hidden, it remains the activesheet and is tested over and over again rather than the sheet pointed to by the ws variable.  Your intent is obviously to test each sheet in turn, so you don't want to use the ActiveSheet as your qualifier.  (this would again be true if you would run the macro with some sheets already hidden (assuming the value in the last cell in column O could have changed value and might warrant being unhidden)).  

Sub HideSheetBasedOnCellValue()

Dim ws As Worksheet
Dim r as Range

For Each ws In ActiveWorkbook.Worksheets
  ' set a range reference to the last filled cell in column O of sheet ws
  set r = ws.Cells(ws.rows.count, "O").End(xlup)
  If r.Value < 500 Then
     ws.Visible = False
  Else: ws.Visible = True
  
  End If
   
Next ws

End Sub

I assume the last cell in column O of each sheet will be a number.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.