应用filter,然后通过macros修改不同的列

参考下表,在“F”栏下有一个filter,其中有以下选项: 实验室,牙科,光学

现在,我需要的是筛选例如“F”列的实验室,然后自动填充“D”列的所有门诊病人。 接下来是再次过滤牙科在“F”列下,然后自动填充“D”列中的所有牙科。 那么同样的过程对于滤光器来说也是一样

捕获

我试图loggingmacros,下面是结果:

Sub test() ActiveSheet.Range("$A$1:$S$2252").AutoFilter Field:=6, Criteria1:="Dental" Range("D3").Select ActiveCell.FormulaR1C1 = "Dental" Selection.FillDown ActiveSheet.Range("$A$1:$S$2252").AutoFilter Field:=6, Criteria1:="Optical" Range("D42").Select ActiveCell.FormulaR1C1 = "Optical" Range("D42").Select Selection.FillDown ActiveSheet.Range("$A$1:$S$2252").AutoFilter Field:=6 End Sub 

但是,我不能使用这个logging,因为我的一些表单有不同的值,例如在单元格“D3”有时是光学的,不像上面的例子。

预先感谢您帮助我。

您可以循环访问一组值,然后逐个提供Range.AutoFilter方法标准。 如前所述,使用xlCellType或xlCellTypeVisible的Range.SpecialCells方法将足以隔离过滤的行。

 Sub meddenlab() Dim a as long, arr As Variant arr = Array("Laboratory", "Dental", "Optical") With Worksheets("Sheet1") If .AutoFilterMode Then .AutoFilterMode = False With .Cells(1, 1).CurrentRegion For a = LBound(arr) To UBound(arr) .AutoFilter field:=6, Criteria1:=arr(a) 'step down one row off the header With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'first check to see if there are visible cells If CBool(Application.Subtotal(103, .Cells)) Then 'there are visible rows - apply the ben typ .Columns(4).SpecialCells(xlCellTypeVisible) = arr(a) End If End With .AutoFilter Next a End With If .AutoFilterMode Then .AutoFilterMode = False End With End Sub 

如果D列中的收益types与F列中的收益types不匹配,则可以使用两个相同大小的数组。 使用第一列作为F列的标准,第二列作为列D的值。