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!
Last edited by ricdamiani; 12-20-2022 at 09:02 PM.
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&, rng, arr(), item As String, fil As Boolean
Sub itema()
Set dic = CreateObject("scripting.dictionary")
lr = Cells(Rows.Count, "C").End(xlUp).Row
rng = Range("C2:C" & lr).Value: fil = False
For i = 1 To UBound(rng)
If Not dic.exists(rng(i, 1)) And Not IsEmpty(rng(i, 1)) Then dic.Add rng(i, 1), ""
With Rows(i + 1)
If .Hidden Then
fil = True
Else
item = .Cells(1, 3).Value
End If
End With
Next
ReDim arr(1 To dic.Count, 1 To 3)
For i = 0 To dic.Count - 1
arr(i + 1, 1) = dic.keys()(i)
If i = 0 Then
arr(i + 1, 2) = dic.keys()(i): arr(i + 1, 3) = dic.keys()(i + 1)
ElseIf i = dic.Count - 1 Then
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i)
Else
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i + 1)
End If
Next
End Sub
Sub NextButton()
itema
With ActiveSheet.Range("A1:V" & lr)
If Not fil Then
.AutoFilter Field:=3, Criteria1:=arr(1, 1)
Exit Sub
End If
For i = 1 To dic.Count
If arr(i, 1) = item Then
.AutoFilter Field:=3, Criteria1:=arr(i, 3)
Exit Sub
End If
Next
End With
End Sub
Sub PrvButton()
itema
With ActiveSheet.Range("A1:V" & lr)
If Not fil Then
.AutoFilter Field:=3, Criteria1:=arr(dic.Count, 1)
Exit Sub
End If
For i = dic.Count To 1 Step -1
If arr(i, 1) = item Then
.Range("A1:V" & lr).AutoFilter Field:=3, Criteria1:=arr(i, 2)
Exit Sub
End If
Next
End With
End Sub
Re: Macro to select the next/previous option in the filter (like a NEXT and PREVIOUS butto
Originally Posted by bebo021999
PHP Code:
Option Explicit
Public dic As Object
Public lr&, i&, rng, arr(), item As String, fil As Boolean
Sub itema()
Set dic = CreateObject("scripting.dictionary")
lr = Cells(Rows.Count, "C").End(xlUp).Row
rng = Range("C2:C" & lr).Value: fil = False
For i = 1 To UBound(rng)
If Not dic.exists(rng(i, 1)) And Not IsEmpty(rng(i, 1)) Then dic.Add rng(i, 1), ""
With Rows(i + 1)
If .Hidden Then
fil = True
Else
item = .Cells(1, 3).Value
End If
End With
Next
ReDim arr(1 To dic.Count, 1 To 3)
For i = 0 To dic.Count - 1
arr(i + 1, 1) = dic.keys()(i)
If i = 0 Then
arr(i + 1, 2) = dic.keys()(i): arr(i + 1, 3) = dic.keys()(i + 1)
ElseIf i = dic.Count - 1 Then
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i)
Else
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i + 1)
End If
Next
End Sub
Sub NextButton()
itema
With ActiveSheet.Range("A1:V" & lr)
If Not fil Then
.AutoFilter Field:=3, Criteria1:=arr(1, 1)
Exit Sub
End If
For i = 1 To dic.Count
If arr(i, 1) = item Then
.AutoFilter Field:=3, Criteria1:=arr(i, 3)
Exit Sub
End If
Next
End With
End Sub
Sub PrvButton()
itema
With ActiveSheet.Range("A1:V" & lr)
If Not fil Then
.AutoFilter Field:=3, Criteria1:=arr(dic.Count, 1)
Exit Sub
End If
For i = dic.Count To 1 Step -1
If arr(i, 1) = item Then
.Range("A1:V" & lr).AutoFilter Field:=3, Criteria1:=arr(i, 2)
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?
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(1, 3).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&, rng, arr(), item As String, fil As Boolean
Sub itema()
Set dic = CreateObject("scripting.dictionary")
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("D5:D" & lr).Value: fil = False
For i = 1 To UBound(rng)
If Not dic.exists(rng(i, 1)) And Not IsEmpty(rng(i, 1)) Then dic.Add rng(i, 1), ""
With Rows(i + 1)
If .Hidden Then
fil = True
Else
item = .Cells(1, "D").Value
End If
End With
Next
ReDim arr(1 To dic.Count, 1 To 3)
For i = 0 To dic.Count - 1
arr(i + 1, 1) = dic.keys()(i)
If i = 0 Then
arr(i + 1, 2) = dic.keys()(i): arr(i + 1, 3) = dic.keys()(i + 1)
ElseIf i = dic.Count - 1 Then
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i)
Else
arr(i + 1, 2) = dic.keys()(i - 1): arr(i + 1, 3) = dic.keys()(i + 1)
End If
Next
End Sub
Sub NextButton()
itema
With ActiveSheet.Range("A4:AN" & lr)
If Not fil Then
.AutoFilter Field:=4, Criteria1:=arr(1, 1)
Exit Sub
End If
For i = 1 To dic.Count
If arr(i, 1) = item Then
.AutoFilter Field:=4, Criteria1:=arr(i, 3)
Exit Sub
End If
Next
End With
End Sub
Sub PrvButton()
itema
With ActiveSheet.Range("A4:AN" & lr)
If Not fil Then
.AutoFilter Field:=4, Criteria1:=arr(dic.Count, 1)
Exit Sub
End If
For i = dic.Count To 1 Step -1
If arr(i, 1) = item Then
.Range("A4:AN" & lr).AutoFilter Field:=4, Criteria1:=arr(i, 2)
Exit Sub
End If
Next
End With
End Sub
Last edited by bebo021999; 12-22-2022 at 09:36 PM.
Bookmarks