1
votes

Good morning to all,

I am a newbie in macros & VBA Excel. I wish to handle hide/unhide rows based on Private Sub Worksheet_Change(ByVal Target As Range). I have the following code event

Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect Password:="avalon"
If Target.Column = 2 Then
Application.EnableEvents = False
Cells(Target.Row, 5).Value = Date + Time
Application.EnableEvents = True
End If
ActiveSheet.Protect Password:="avalon"
End Sub

The idea is to aggregate this event (hide/unhide rows) when I entry data from 'B4'to 'B21' (Sometimes they will be filled totally and sometimes partially. It does not matter). Maybe, clicking in 'B21'or another event using double click in certain cell to activate unhide rows from 22 to 36 for follow up fill those rows. I hope to be clear, if not let me know to try to clarify my need to discover the proper code based on events.

Thanks in advance. I promise to learn quickly.

enter image description here

1
So what, you enter data in B4 then the entire 4 row becomes hidden?K.Dᴀᴠɪs
Hello. It is true, I forgot to comment that always is unhide from row 2 to 21.If I need a second Bag labels I will need to unhide 22 to 36 to follow fill the second bag labels. I hope to be specific and understandable. Thanks.JOSE PEDRO MUÑIZ VARGAS
K. Davis. I entry in cell B4 a chain of alphanumeric code to verify a label shipment. E.G. 190453---1|68|1. Thanks.JOSE PEDRO MUÑIZ VARGAS
Still not quite sure I understand exactly what you are wanting. Fill in the blanks.... When I do [____], then I want [____] to happen.K.Dᴀᴠɪs
Thank you, K. Davis for your interest to help me. I ran alternative solution...I actually have hide from row 22 to 51. When I am filling the visible rows by default from row 2 to 21 and, later of this I have the necessity to fill another Bag labels field (That will be #2), then, I will need to unhide from row 22 to 36; It could be possible to fill those rows, like bag labels #1 case and, I will need to unhide from row 37 to 51.The idea is to keep hide bag labels #2 (Row 22 to 36) and #3 (row 37 to 51) at least we need to unhide each one. ThanksJOSE PEDRO MUÑIZ VARGAS

1 Answers

1
votes

It seems that what you are trying to do is to automatically hide or show rows based on the selection.

You can show or hide rows by using the .hidden on a range object (using EntireRow). You just need some way of determining when to hide these rows. By using the selectionChange event, you can show or hide rows based on what rows are currently selected.

The following code, when put on a worksheet object, will help you.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Row
    Case 21
        ActiveSheet.Range("B22:B36").EntireRow.Hidden = False
    Case 22 To 36
        ActiveSheet.UsedRange.EntireRow.Hidden = False
    Case Else
        ActiveSheet.Range("B22:B36").EntireRow.Hidden = True
End Select
End Sub

You can create more cases and give the row numbers that, when selected, will hide or show other ranges.

I hope I understood your question correctly.