Vba 取过滤列的平均值,并让宏在消息框中显示它
此宏在Excel文档中运行并过滤出3行。现在,当过滤第三行时,宏就停止了,但是我想取第三行的平均值,它有一个动态范围,并将其显示在消息框中Vba 取过滤列的平均值,并让宏在消息框中显示它,vba,excel,macros,average,Vba,Excel,Macros,Average,此宏在Excel文档中运行并过滤出3行。现在,当过滤第三行时,宏就停止了,但是我想取第三行的平均值,它有一个动态范围,并将其显示在消息框中 Sub PriceVerifyMacro() Dim retval retval = InputBox("Please Enter The Price Book Header") If IsNumeric(retval) = False Then MsgBox "You didn't enter a number
Sub PriceVerifyMacro()
Dim retval
retval = InputBox("Please Enter The Price Book Header")
If IsNumeric(retval) = False Then
MsgBox "You didn't enter a number! Try again"
Exit Sub
End If
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=19, Criteria1:="=" & retval
Dim retval1
retval1 = InputBox("Please Enter The Net Weight")
If IsNumeric(retval1) = False Then
MsgBox "You didn't enter a number! Try again"
Exit Sub
End If
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=40, Criteria1:="=" & retval1
Dim retval2
retval2 = InputBox("Please Enter The PO Cost")
If IsNumeric(retval2) = False Then
MsgBox "You didn't enter a number! Try again"
Exit Sub
End If
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=70, Criteria1:="=" & retval2
If MsgBox("Would you like to reset the filters?", vbYesNo) = vbNo Then Exit Sub
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=70
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=40
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=19
End Sub
<> p>你需要在代码中得到它。
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=70, Criteria1:="=" & retval2
For Each cell In Range(Cells(1, 70), Cells(293662, 70)).SpecialCells(xlCellTypeVisible) 'this could be improved, why set to 293662?
'your stuff to get average and msgbox
Next
If MsgBox("Would you like to reset the filters?", vbYesNo) = vbNo Then Exit Sub
你可以用这个。在应用第三个过滤器后添加此选项(Dim
行当然可以移到顶部):
可以上述方法不起作用吗?您是否收到任何错误,如果是,在哪里?它是否运行,但不完全执行您想要的操作,等等?请详细说明您的问题/问题,并请阅读。@BruceWayne对于措辞拙劣的问题表示抱歉。我试图修改它,以明确我想要什么。一切正常,我似乎无法找到如何获得动态范围的平均值,而不是找到固定范围的平均值。@trincot为了澄清这一点,我使用了一个大型项目摘要,其中我必须隐藏某些列,以使文档更易于查看。我试图得到第70行的平均值,也就是说,有任何答案解决了你的问题吗?你能留下评论吗?
Dim sum As Double
Dim count As Double
Dim avg As Double
Dim cell As Range
sum = 0
count = 0
avg = 0
For Each cell In Range("BR2:BR" & ActiveSheet.UsedRange.Rows.count).SpecialCells(xlCellTypeVisible)
sum = sum + cell.Value
count = count + 1
Next
If count > 0 Then avg = sum / count
MsgBox "Average is " & avg