Macro to hide and unhide multiple rows at a time

tlc53

Active Member
Joined
Jul 26, 2018
Messages
333
Hi there,
I have a table of 40 lines in which I manually hide/unhide depending on the amount of data needing to go in (40 lines max).
I would like to have two macro buttons so that I can hide/unhide 5 rows at a time. So 1-5 would always show, add +5 line button and rows 1-10 would show, add +5 button again and 1-15 would show. Then should I want to take 5 lines away, -5 button would go back to showing 1-10. However, if the cells contain data, they should not be able to be hidden.
C37:C76 are the entry cells.
Bit of a big ask but fingers crossed.
Thank you! :biggrin:
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
2,650
Try this :
Code:
Sub ShowFive()
Dim rng As Range
On Error Resume Next
Set rng = [C42:C76].SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
    [C42:C46].EntireRow.Hidden = False
ElseIf rng.Count = 35 Then
    Exit Sub
Else: rng(rng.Count + 1, 1).Resize(5).EntireRow.Hidden = False
End If
End Sub


Sub HideFive()
Dim r%
For r = 76 To 42 Step -5
    If WorksheetFunction.CountA(Range(Cells(r - 4, "C"), Cells(r, "C"))) <> 0 Then
        Exit For
    Else
        Rows(r - 4 & ":" & r).Hidden = True
    End If
Next
End Sub
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,664
Here's a quick, untested forms control button code for the +5. If it works for you, you can adapt it for the -5 button. Put the button in rows 1-5 and se**** properties to not change size or move with cells.
Code:
Sub Button1_Click()
'+5 button
Dim N As Variant
N = ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).EntireRow.Count
Application.ScreenUpdating = False
If Not IsError(N) And N < Rows.Count - 5 Then
    Rows("6:" & Rows.Count).Hidden = True
    Rows("6:" & N + 5).Hidden = False
End If
Application.ScreenUpdating = True
End Sub
 

tlc53

Active Member
Joined
Jul 26, 2018
Messages
333
Absolutely brilliant!! Works a treat!
I'm so happy. Thank you!! :)
 

tlc53

Active Member
Joined
Jul 26, 2018
Messages
333
When I posted my gratitude, I had only seen the answer from footoo at the time.
However, yours works too, thanks!! :)
Although your code was shorter, I ended up going with footoo because I could understand the code easier and adapt it with the changing cell references.

Thanks again!
 

Forum statistics

Threads
1,081,543
Messages
5,359,431
Members
400,526
Latest member
Brook1083

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top