我有以下VBA代码:
Function AvgNoColor(rngData As Range, color As String) As Variant
Dim sum As Double
Dim amount As Double
Dim C As Range
For Each C In rngData
If color = "Red" Then
If Not COLORINDEX(C) = 44 And Not COLORINDEX(C) = 3 Then
sum = sum + C.Value
amount = amount + 1
Else
'HERE
AvgNoColor = ""
End If
ElseIf color = "Orange" Then
If Not COLORINDEX(C) = 44 Then
sum = sum + C.Value
amount = amount + 1
Else
'HERE
AvgNoColor = ""
End If
End If
Next
AvgNoColor = sum / amount
End Function
简单地说,我希望单元格的平均值在一定范围内,而不是颜色"红色"或" Orange"。平均值按预期返回,但是当单元格需要为空时,单元格会出现#VALUE
错误。
答案 0 :(得分:1)
通过将此行(search = params[:search].present? ? params[:search] : "*"
#where = {order: {created_at: :desc}}
#This kept throwing an unknown where operand error so I changed it
#Also can't put misspellings: false here since it keeps returning empty results
where = {}
if params[:date_filter].present?
where[:date] = {
gte: DateTime.strptime(params[:date_filter], '%m/%d/%Y %l:%M %p').beginning_of_day,
lte: DateTime.strptime(params[:date_filter], '%m/%d/%Y %l:%M %p').end_of_day
}
end
#Put the order here, as well as the misspelling option since putting it at the top returned empty results.
@ingoing = Document.search( search, where: where.merge(:outgoing => false), order: {created_at: :desc}, misspellings: false )
@outgoing = Document.search( search, where: where.merge(:outgoing => true), order: {created_at: :desc}, misspellings: false )
)放在if-else块之外,您已将代码打开为零/未定义,如果触发,将在每个方向上抛出错误。
尝试此修订:
AvgNoColor = sum / amount
答案 1 :(得分:0)
尝试ISERROR(值)功能:
If Not IsError(AvgNoColor) Then
'your code
End If
如果value是错误值(#N / A,#VALUE!,#REF!,#DIV / 0!,#NUM!,#NAME?或#NULL),则此函数将返回TRUE。否则,它将返回FALSE。
答案 2 :(得分:0)
请尝试细胞
=IF(ISERROR(AvgNoColor(A3:A14,"Red"))="FALSE",AvgNoColor(A3:A14,"Red"),"")