使用excel宏循环过滤选项

时间:2021-01-14 02:31:06

In excel File A, Col A i have family type for a parts list. 0.5V, 2V, 5V,10V,12V, etc in Col B thru K I have some part data dimensions and things like that. I was looking for a way to have a macro run a filter by the values in Col A, then run other code I already have, clear the filter selection, and then go to the selection option in the filter.

在excel文件A中,Col A我有零件清单的家族类型。0。5V, 2V, 5V,10V,12V,等等在Col B到K中我有一些数据尺寸之类的。我在寻找一种方法,让宏通过Col a中的值运行一个筛选器,然后运行我已经有的其他代码,清除筛选器选择,然后转到筛选器中的选择选项。

so if my filter options were

如果我的过滤器选项是。

.1
.5
.03
.9
2
8

it would filter for .1 , run code, clear filter, filter by .5, run code, clear filter, filter by .03 etc etc. I am total lost on how to cycle thru the filter list. Any help would be much appreciated

它会过滤。1,运行代码,清除过滤器,过滤。5,运行代码,清除过滤器,过滤。03等等。如有任何帮助,我们将不胜感激

1 个解决方案

#1


1  

create an array of the values then cycle through that array.¹

创建一个数组的值然后循环数组。¹

Sub sequentialAutoFilter()
    Dim a As Long, arrs As Variant

    arrs = Array(0.1, 0.5, 0.03, 0.9, 2, 8)

    With Worksheets("Sheet99")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For a = LBound(arrs) To UBound(arrs)
                .AutoFilter field:=1, Criteria1:=arrs(a)
                With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                    If CBool(Application.Subtotal(103, .Cells)) Then
                        'there are filtered cells visible, run code here
                        'note: .Cells does not include the header row here
                    End If
                End With
            Next a
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

End Sub

¹ Quoted from Scott Craner's comments.

¹引用斯科特起重机的言论。

#1


1  

create an array of the values then cycle through that array.¹

创建一个数组的值然后循环数组。¹

Sub sequentialAutoFilter()
    Dim a As Long, arrs As Variant

    arrs = Array(0.1, 0.5, 0.03, 0.9, 2, 8)

    With Worksheets("Sheet99")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For a = LBound(arrs) To UBound(arrs)
                .AutoFilter field:=1, Criteria1:=arrs(a)
                With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                    If CBool(Application.Subtotal(103, .Cells)) Then
                        'there are filtered cells visible, run code here
                        'note: .Cells does not include the header row here
                    End If
                End With
            Next a
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

End Sub

¹ Quoted from Scott Craner's comments.

¹引用斯科特起重机的言论。