Macro to filter one row at a time consecutively

David28

New Member
Joined
Jul 2, 2018
Messages
5
I want to create a macro where each time I click the macro button I place in row 1 it filters to only display one row at a time under row 1. So for example when clicked the first time only rows 1 and 2 would be visible, when clicked again only rows 1 and 3 would be visible, etc. How would I create this macro? Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi,

A couple of event macros ...

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim Onecell As Range
Dim counter As Long
Dim rng As Range


Set rng = Range("A2:A10")
' Cell M1 keeps track of your counter
counter = [M1] + 1
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    For Each Onecell In rng
        rng.EntireRow.Hidden = True
        If Onecell.Row = counter + 1 Then Onecell.EntireRow.Hidden = False: Exit For
    Next Onecell
[M1] = counter
Cancel = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub




Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
' To Unhide Rows
  Cells.EntireRow.Hidden = False
  [M1] = 0
  Cancel = True
End Sub

Hope this will help
 
Upvote 0
The next macro needs a cell in row 1 to perform the calculations of the row to be filtered. Please change "Z" for any available column you have.


Code:
Sub Macro7()
    Dim lr As Long, r As Variant, col As String
    Application.ScreenUpdating = False
    col = "Z"
    Cells.EntireRow.Hidden = False
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    lr = Cells.Find("*", Cells(1), , , xlByRows, xlPrevious, , , False).Row
    r = Cells(1, col).Value
    If r = "" Or r < 2 Or Not IsNumeric(r) Then r = 2
    Rows("2:" & lr).EntireRow.Hidden = True
    Rows(r).EntireRow.Hidden = False
    Cells(1, col).Value = r + 1
    If Cells(1, col).Value > lr Then Cells(1, col).Value = 2
End Sub
 
Upvote 0
Another option
Code:
Sub David28()
   Static Rw As Long
   Rows("2:" & Rows.Count).Hidden = True
   If Rw = 0 Then Rw = 2
   Rows(Rw).Hidden = False
   If Cells(Rw + 1, 1) = "" Then Rw = 2 Else Rw = Rw + 1
End Sub
This assumes that col A will always have a value
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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