worksheetfunction.aggregate忽略hidden / error / 0值

时间:2015-06-08 05:02:04

标签: excel vba excel-vba

此代码有效,直到循环遇到空白/错误单元格值

Dim ws1 As Worksheet, wstest As Worksheet, wskb As Worksheet
Dim lastrowinc As Long, curRow As Long, lastrowkb As Long
Dim medianvalue As Integer
Dim critvalue As String


'Sheet to look up Critvalue
Set ws1 = Sheets("Incident")

'Sheet where to store Medianvalue
Set wstest = Sheets("Sheet3")

'Sheet where to get Critvalue (A2 onwards)
Set wskb = Sheets("KB Articles")

'For autofilter criteria
lastrowinc = ws1.Range("A" & Rows.Count).End(xlUp).Row


lastrowkb = wskb.Range("A" & Rows.Count).End(xlUp).Row

For curRow = 2 To lastrowkb

critvalue = wskb.Range("A" & curRow).Value

ws1.Range("M1:A" & lastrowinc).AutoFilter field:=13, Criteria1:=critvalue



medianvalue = WorksheetFunction.Aggregate(12, 5, Columns(2))


wstest.Range("A" & Rows.Count).End(xlUp).Offset(1) = medianvalue


Next curRow

现在根据this page我们可以使用7作为Arg2来忽略隐藏和错误。我尝试了但是它仍然在这一行上给出错误1004:medianvalue = WorksheetFunction.Aggregate(12, 5, Columns(2)) (注意我试过5和7,但仍然是同样的错误)。我甚至试图设置所有空白单元格以产生excel错误(#N / A),但无济于事。它可以工作,直到遇到BLANK /错误单元

我对结果输出也有疑问。为什么显示整数?我之前使用手动自动过滤器标准(使用texbox),它显示一个带小数的值,但是当我将它循环整数时。

谢谢你们。

1 个答案:

答案 0 :(得分:1)

您可以使用Count函数测试是否存在至少一个数字值。

If WorksheetFunction.Count(ws1.Columns(2).SpecialCells(xlCellTypeVisible)) > 0 Then
    medianvalue = WorksheetFunction.Aggregate(12, 5, ws1.Columns(2))
Else
    medianvalue = some value that is meaningful to you
End If