help with hiding rows when a cell is not blank

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to hide rows in the below code, but for some reason is it not working.

Case 59 is the line I am wanting to use to hide rows if a cell is not blank.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim hr As Double
hr = ActiveCell.Row
Debug.Print hr

  ' If Sheets("Rapid Receipting").Range("AB57") <> "" Then Rows("1:" & hr).EntireRow.Hidden = True
    
    If Intersect(Target, Columns("AA")) Is Nothing Then Exit Sub 'stop if you ddin't right click in the AA-column

     'later instead of that msgbox, you do target.offset(......).COPY

     Select Case Target.Row Mod 60 'depending on the rownumber (modulus 60 !!), choose a range
          Case 2: Target.Offset(, 2).Resize(16, 2).Copy
          Case 19 To 20: Target.Offset(, 2).Resize(1, 47).Copy
          Case 21: Target.Offset(, 2).Resize(1, 31).Copy
          Case 24, 37: Target.Offset(, 2).Resize(12, 3).Copy
          Case 50: Target.Offset(, 2).Resize(8, 3).Copy
          Case 59: If Range("AC59").Value <> "" Then Rows("1:" & Target.Offset(, 2).Row).EntireRow = Hidden
                    Case Else: Exit Sub
     End Select
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The way you currently have it the code should be crashing.

Change your Case 59 to this
VBA Code:
              Case 59: If Range("AC59").Value <> "" Then Rows("1:" & target.Offset(, 2).Row).EntireRow.Hidden = True
 
Upvote 0
Okay, I have done a work around that works for me.

VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    
     If Intersect(Target, Columns("AA")) Is Nothing Then Exit Sub 'stop if you ddin't right click in the AA-column

     'later instead of that msgbox, you do target.offset(......).COPY

     Select Case Target.Row Mod 60 'depending on the rownumber (modulus 60 !!), choose a range
          Case 58: Rows("1:" & ActiveCell.Row).EntireRow.Hidden = True
          Case Else: Exit Sub
     End Select

     Cancel = True 'cancel the normal popup for right-clicking
    
End Sub

But I will also try what you have written Alex:) thanks
 
Upvote 0
Solution
VBA Code:
 Case 59: If Range("AC59").Value <> "" Then Rows("1:" & Target.Offset(, 2).Row).EntireRow = Hidden
because of that "Select Case Target.Row Mod 60", the cell you're looking at isn't always row 59, but can also be every multiple 60+59, so 59, 119,179,239,299,...
so it has to be something like
Case 59: If Range("AC" & target.row).Value <> "" Then Rows(target.row-58 & ":" & Target.Offset(, 2).Row).EntireRow = Hidden
 
Upvote 0
But I will also try what you have written Alex:) thanks
You have already made that change in the code you showed in that same post (#3).
If you adopt @BSALV's Case 59 code change in the above post you will need to apply the same amendment, it is using your original incorrect code.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,845
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top