1

I am using the following code in a workbook, to hide rows, before save, if a value is populated into a specific column. The information on the worksheet in question comes from a separate worksheet. This code works great to hide rows, however, I would like to add code to unhide rows if a value is added/changed in the original worksheet where the info is coming from upon save. Is this possible and how would I go about it?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim beginRow As Long, endRow As Long, chkCol As Long, rowCnt As Long
  Dim rngResult As Range
  Dim ws As Worksheet

  beginRow = 3
  endRow = 38
  chkCol = 14

  Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")

  For rowCnt = endRow To beginRow Step -1
    If Cells(rowCnt, chkCol).Value = "X" Then
      ws.Cells(rowCnt, chkCol).EntireRow.Hidden = True
    End If
  Next rowCnt

End Sub
6
  • How are rows in this sheet related to rows in the source sheet? Where/what are the values you want to reference on the other sheet? Commented Nov 8, 2019 at 4:24
  • 1
    You say "Hide" in the text but your code is Deleting rows - it's not clear how those deleted rows could be "unhidden" ? Commented Nov 8, 2019 at 6:28
  • Please update your question to switch out the code - it's too difficult to read in a comment Commented Nov 8, 2019 at 15:56
  • My workbook contains a document that will be completed by staff to request reimbursement for travel. The worksheet that I am working off of copies the fields to a new worksheet so that the data can be a 'table'. This table will result in the ability to create a pivot table to summarize the information for ease of data entry into our payroll system. Blank rows are hidden upon the employees selection to 'save' the file. However, if the employee realizes they missed something and makes a correction and saves again, the rows are not 'unhidden' so the data can be populated.
    – Kristen
    Commented Nov 8, 2019 at 16:05
  • There's an "edit" link under your post... Commented Nov 8, 2019 at 16:07

1 Answer 1

1

If I'm understanding correctly:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

  Const beginRow as Long = 3  '<< use Const for fixed values
  Const endRow as Long = 38
  Const chkCol as Long = 14

  Dim rowCnt As Long
  Dim ws As Worksheet

  Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")

  For rowCnt = endRow To beginRow Step -1
       With ws.Cells(rowCnt, chkCol) 
           .EntireRow.Hidden = (.Value = "X")
       End With
    End If
  Next rowCnt

End Sub

EDIT: to clarify - the code above is a shorter version of something like this

   With ws.Cells(rowCnt, chkCol)
       If .Value = "X" Then 
           .EntireRow.Hidden = True
       Else
           .EntireRow.Hidden = False     ' Edited: corrected Syntax 
       End If
   End With
3
  • What is the additional code that allows the rows to 'unhide'? I'm not sure I see it? I want to be sure that I understand what is being done?
    – Kristen
    Commented Nov 8, 2019 at 16:29
  • Hidden will be set to True or False depending on whether (.Value = "X") is True or False When the value is not "X" hidden will be False, and the row will be visible again. Commented Nov 8, 2019 at 17:10
  • 1
    Okay... that makes sense! :) Thank you for your help! I do want to understand this stuff too... not just have someone else provide the coding. I appreciate your explanation.
    – Kristen
    Commented Nov 8, 2019 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.