• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How do I hide rows based on cell values when closing the workbook

RPeck

New Member
I would like to hide the entire row when the value in column G for that row is equal to "Yes". I only want this macro to run for the open sheet and I would like it to run when the workbook is closed. I have seen similar requests, but I cannot make them work for me. All help is appreciated.
__________________________________________________________________
Mod edit : thread moved to appropriate forum !
 

Attachments

  • 2016 Outstanding Contract Issues 002.xlsm
    598.7 KB · Views: 3
Hi RPeck, and welcome to the forum. :awesome:

In the future, it's helpful to provide the password to unlock the sheets, which is needed in order for macro to run. I didn't bother to crack it, so this code is untested but should work. Make sure you change the myPass variable to the real password.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim c As Range
Dim rngItems As Range
Dim saveState As Boolean
'IMPORTANT!!! Put the sheet protection password here
Const myPass As String = "abc123"

'Which worksheet to deal with?
Set ws = ActiveSheet

'Note if we should auto save, or user cancelled
saveState = ThisWorkbook.Saved

Application.ScreenUpdating = False
With ws
    .Unprotect myPass
    'Look for cells that have been filled in and are visible
    On Error Resume Next
    Set rngItems = .Range("G1:G" & .Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeConstants).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
   
    If Not rngItems Is Nothing Then
        For Each c In rngItems
            c.EntireRow.Hidden = (UCase(c.Value) = "YES")
        Next c
    End If
    .Protect myPass
End With

If saveState Then
    ThisWorkbook.Save
End If

End Sub
 
The code has no issues. I think you might have it in wrong place (I.E. in standard module instead of workbook).

First, go to VBA Project and remove any unnecessary Modules.

Then double click on "ThisWorkbook"
upload_2016-3-8_22-53-6.png

Paste in Luke's code and change the password.
 
Back
Top