我正在尝试计算过滤数据中特定字符串的出现次数。我可以使用单元格中的公式来完成,但是当我将其与工作簿中的其他宏组合在一起时,整个事情就会冻结。Excel中过滤数据中的CountIF
所以我想将计算移动到VBA,以便它只在宏运行时进行计算。下面是在细胞中的工作原理的公式:
=SUMPRODUCT(SUBTOTAL(3,OFFSET('2015 Master'!H:H,ROW('2015 Master'!H:H)-MIN(ROW('2015 Master'!H:H)),,1)),ISNUMBER(SEARCH("*Temp*",'2015 Master'!H:H))+0)
基本上我要计数的次数“TEMP”发生在H列,但只在滤波后的数据。
谢谢你的帮助!
ADDITION:
这是我为宏编写的代码到目前为止。它过滤不同工作表上的数据,然后使用日期范围更新数据透视表。我想将计数计算添加到此代码的末尾,并将计数返回到“报告”表中的单元格。
子的button1_Click() “刷新枢轴表,并在活性片
ActiveWorkbook.RefreshAll
'Gather the start and end times from the active sheet
dStart = Cells(2, 5).Value
dEnd = Cells(3, 5).Value
'Change the active sheet to the alarms database, clear all filters and then filter for the defined date range and filter for only GMP alarms
Sheets("2015 Master").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or ActiveWorkbook.ActiveSheet.AutoFilterMode Then
ActiveWorkbook.ActiveSheet.ShowAllData
End If
ActiveSheet.ListObjects("Table44").Range.AutoFilter Field _
:=3, Criteria1:=">=" & dStart, Operator:=xlAnd, Criteria2:= _
"<=" & dEnd
Range("Table44[[#Headers],[GMP or non-GMP]]").Select
ActiveSheet.ListObjects("Table44").Range.AutoFilter Field:=2, Criteria1:= _
"GMP"
'Change the active sheet to the Reporting sheet
Sheets("Reporting").Select
'Within the alarms pivot table clear the label filters then filter for the date range and GMP alarms
ActiveSheet.PivotTables("PivotTable1").PivotFields("Active Time"). _
ClearLabelFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Active Time").PivotFilters. _
Add Type:=xlDateBetween, Value1:=dStart, Value2:=dEnd
ActiveSheet.PivotTables("PivotTable1").PivotFields("GMP or non-GMP"). _
CurrentPage = "GMP"
End Sub
那怎么假设VBA宏想知道,如果被过滤或没有数据?请澄清。最好的问候, –
看到我的问题 –
此外代码很好。我只想添加到它。我想要计算在H列'2015 Master'表中过滤的数据中显示的“temp”的次数。这是过滤的位置:ActiveSheet.ListObjects(“Table44”)。Range.AutoFilter Field (“Table44 [[#Headers],[GMP or non-GMP]),或者是不符合GMP标准的产品,或者不符合GMP要求的产品,或者不符合GMP标准。 ]] “)选择 ActiveSheet.ListObjects(” Table44" )Range.AutoFilter场:= 2,标准1:= _ “GMP” –