Jun 01 2023 06:42 PM
My worksheet has numerous rows that may not be used each particular time I open it. I want to be able to keep the descending rows hidden until text is inserted into the row above it. That is, say only row A6 is visible on opening the worksheet., and rows A7.A100 are hidden. When the user enters text in A6, only then will A7 automatically become unhidden, and so forth down the page. Is there a way to set this up, say a formula in conditional formatting?
Jun 02 2023 04:38 AM
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
r = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.EnableEvents = False
Cells(r + 1, 1).EntireRow.Hidden = False
Range(Cells(r + 2, 1), Cells(Rows.Count, 1)).EntireRow.Hidden = True
Application.EnableEvents = True
End Sub
Switch back to Excel.
Save the workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open the workbook.
Warning: the code will disable Undo.
Jan 10 2024 11:14 PM
Jan 11 2024 03:04 AM
Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar? Alternatively, you can attach it to a private message to me. Thanks in advance.
Jan 11 2024 02:37 PM
I received your private message.
The cause of the problem is that you have formulas in G5:G200, so the code always sees row 200 as the last row.
The following should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
r = Range("A:F").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.EnableEvents = False
Cells(r + 1, 1).EntireRow.Hidden = False
Range(Cells(r + 2, 1), Cells(Rows.Count, 1)).EntireRow.Hidden = True
Application.EnableEvents = True
End Sub
Jan 13 2024 12:19 AM
Hans, the code works perfectly! Thanks for your help! Now, is there some code that I can add to your code to unprotect the spreadsheet, then unhide a line, and then protect the sheet again? I tried adding code myself but of course it did not work...I am not very good. Thanks for your help again.
Frank
Jan 13 2024 12:57 AM
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
r = Range("A:F").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.EnableEvents = False
Me.Unprotect Password:="secret"
Cells(r + 1, 1).EntireRow.Hidden = False
Range(Cells(r + 2, 1), Cells(Rows.Count, 1)).EntireRow.Hidden = True
Me.Protect Password:="secret"
Application.EnableEvents = True
End Sub
Replace secret with the password used to protect the sheet.
If you want to protect the sheet without setting a password, omit both occurrences of Password:="secret"
Jan 14 2024 03:21 AM
Hans, I sent you a private message with a copy of my spreadsheet because I could not get the code to work. Never mind...I figured out the problem and it works correctly! Thanks for your help!