+ Reply to Thread
Results 1 to 5 of 5

Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS button)

  1. #1
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Exclamation Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS button)

    Looking macros that would select the next/previous option in the filter.

    For example, in the attached sheet, 'ITEM 1' is selected, and if you click in the drop down filter in column C, next option down is 'ITEM 10', so the macro would select 'ITEM 10'. If 'ITEM 10' is selected, the macro would select the next option, which is 'ITEM 11', and so on. This macro would be assined to the NEXT button.

    The PREVIOUS button would have a macro that does the reverse (would select the previous option).

    Thanks all!
    Attached Files Attached Files
    Last edited by ricdamiani; 12-20-2022 at 09:02 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS butto

    On your Next button, right click -> Assign Macro and put this into Macro Name:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Then right click Previous -> Assign Macro and put this in:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Then put this in a standard module:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,662

    Re: Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS butto

    PHP Code: 
    Option Explicit
    Public dic As Object
    Public lr&, i&, rngarr(), item As Stringfil As Boolean
    Sub itema
    ()
    Set dic CreateObject("scripting.dictionary")
    lr Cells(Rows.Count"C").End(xlUp).Row
    rng 
    Range("C2:C" lr).Valuefil False
    For 1 To UBound(rng)
        If 
    Not dic.exists(rng(i1)) And Not IsEmpty(rng(i1)) Then dic.Add rng(i1), ""
        
    With Rows(1)
            If .
    Hidden Then
                fil 
    True
            
    Else
                
    item = .Cells(13).Value
            End 
    If
        
    End With
    Next
    ReDim arr
    (1 To dic.Count1 To 3)
    For 
    0 To dic.Count 1
        arr
    (11) = dic.keys()(i)
        If 
    0 Then
            arr
    (12) = dic.keys()(i): arr(13) = dic.keys()(1)
        ElseIf 
    dic.Count 1 Then
            arr
    (12) = dic.keys()(1): arr(13) = dic.keys()(i)
        Else
            
    arr(12) = dic.keys()(1): arr(13) = dic.keys()(1)
        
    End If
    Next
    End Sub
    Sub NextButton
    ()
    itema
    With ActiveSheet
    .Range("A1:V" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=3Criteria1:=arr(11)
            Exit 
    Sub
        End 
    If
        For 
    1 To dic.Count
            
    If arr(i1) = item Then
                
    .AutoFilter Field:=3Criteria1:=arr(i3)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub
    Sub PrvButton
    ()
    itema
    With ActiveSheet
    .Range("A1:V" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=3Criteria1:=arr(dic.Count1)
            Exit 
    Sub
        End 
    If
        For 
    dic.Count To 1 Step -1
            
    If arr(i1) = item Then
                
    .Range("A1:V" lr).AutoFilter Field:=3Criteria1:=arr(i2)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub 
    Attached Files Attached Files
    Quang PT

  4. #4
    Forum Contributor
    Join Date
    07-28-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365
    Posts
    298

    Re: Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS butto

    Quote Originally Posted by bebo021999 View Post
    PHP Code: 
    Option Explicit
    Public dic As Object
    Public lr&, i&, rngarr(), item As Stringfil As Boolean
    Sub itema
    ()
    Set dic CreateObject("scripting.dictionary")
    lr Cells(Rows.Count"C").End(xlUp).Row
    rng 
    Range("C2:C" lr).Valuefil False
    For 1 To UBound(rng)
        If 
    Not dic.exists(rng(i1)) And Not IsEmpty(rng(i1)) Then dic.Add rng(i1), ""
        
    With Rows(1)
            If .
    Hidden Then
                fil 
    True
            
    Else
                
    item = .Cells(13).Value
            End 
    If
        
    End With
    Next
    ReDim arr
    (1 To dic.Count1 To 3)
    For 
    0 To dic.Count 1
        arr
    (11) = dic.keys()(i)
        If 
    0 Then
            arr
    (12) = dic.keys()(i): arr(13) = dic.keys()(1)
        ElseIf 
    dic.Count 1 Then
            arr
    (12) = dic.keys()(1): arr(13) = dic.keys()(i)
        Else
            
    arr(12) = dic.keys()(1): arr(13) = dic.keys()(1)
        
    End If
    Next
    End Sub
    Sub NextButton
    ()
    itema
    With ActiveSheet
    .Range("A1:V" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=3Criteria1:=arr(11)
            Exit 
    Sub
        End 
    If
        For 
    1 To dic.Count
            
    If arr(i1) = item Then
                
    .AutoFilter Field:=3Criteria1:=arr(i3)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub
    Sub PrvButton
    ()
    itema
    With ActiveSheet
    .Range("A1:V" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=3Criteria1:=arr(dic.Count1)
            Exit 
    Sub
        End 
    If
        For 
    dic.Count To 1 Step -1
            
    If arr(i1) = item Then
                
    .Range("A1:V" lr).AutoFilter Field:=3Criteria1:=arr(i2)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub 
    The macro is working perfectly in the 'Example 1.1' sheet, but it doesn't work when I put into my real sheet (I did some modifications to the real one since I posted the 'Example 1.1' sheet).

    I have attached the current sheet with the current structure, are you able to modify the macro reflecting the new file please?

    Thank you, appreciate your help!
    Attached Files Attached Files

  5. #5
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,662

    Re: Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS butto

    Are you looking for filtering column D?
    If so, try to replace

    PHP Code: 
    item = .Cells(13).Value 
    with
    PHP Code: 
    item = .Cells(1"D").Value 
    and

    PHP Code: 
    .AutoFilter Field:=3
    with
    PHP Code: 
    .AutoFilter Field:=4
    And the final code should be:
    PHP Code: 
    Option Explicit
    Public dic As Object
    Public lr&, i&, rngarr(), item As Stringfil As Boolean
    Sub itema
    ()
    Set dic CreateObject("scripting.dictionary")
    lr Cells(Rows.Count"D").End(xlUp).Row
    rng 
    Range("D5:D" lr).Valuefil False
    For 1 To UBound(rng)
        If 
    Not dic.exists(rng(i1)) And Not IsEmpty(rng(i1)) Then dic.Add rng(i1), ""
        
    With Rows(1)
            If .
    Hidden Then
                fil 
    True
            
    Else
                
    item = .Cells(1"D").Value
            End 
    If
        
    End With
    Next
    ReDim arr
    (1 To dic.Count1 To 3)
    For 
    0 To dic.Count 1
        arr
    (11) = dic.keys()(i)
        If 
    0 Then
            arr
    (12) = dic.keys()(i): arr(13) = dic.keys()(1)
        ElseIf 
    dic.Count 1 Then
            arr
    (12) = dic.keys()(1): arr(13) = dic.keys()(i)
        Else
            
    arr(12) = dic.keys()(1): arr(13) = dic.keys()(1)
        
    End If
    Next
    End Sub
    Sub NextButton
    ()
    itema
    With ActiveSheet
    .Range("A4:AN" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=4Criteria1:=arr(11)
            Exit 
    Sub
        End 
    If
        For 
    1 To dic.Count
            
    If arr(i1) = item Then
                
    .AutoFilter Field:=4Criteria1:=arr(i3)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub
    Sub PrvButton
    ()
    itema
    With ActiveSheet
    .Range("A4:AN" lr)
        If 
    Not fil Then
            
    .AutoFilter Field:=4Criteria1:=arr(dic.Count1)
            Exit 
    Sub
        End 
    If
        For 
    dic.Count To 1 Step -1
            
    If arr(i1) = item Then
                
    .Range("A4:AN" lr).AutoFilter Field:=4Criteria1:=arr(i2)
                Exit 
    Sub
            End 
    If
        
    Next
    End With
    End Sub 
    Last edited by bebo021999; 12-22-2022 at 09:36 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. macro which should select to the previous sheettabs
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2020, 01:38 AM
  2. Replies: 5
    Last Post: 02-19-2016, 06:37 PM
  3. Select next/previous item from data validation list using Command Button
    By msn.niyasu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2014, 07:26 AM
  4. [SOLVED] Userform_initialize, select Multipage tab based on which previous userform button pressed?
    By jonathan@ibbotsons in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2013, 01:58 AM
  5. Select previous sheet Macro
    By Leo7724 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2013, 06:22 AM
  6. Select previous cell using macro
    By DJGodders in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2012, 11:18 AM
  7. Pivot Filter Macro - Auto Update to select previous 30 days.
    By rocksolid77 in forum Excel General
    Replies: 2
    Last Post: 11-26-2012, 01:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1