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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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