我有这个代码,我一直用它来查找每小时和每月 50% 的超额值。附件是数据的图片。Gen 列中有一些 NULL 值,我需要代码跳过这些行,否则结果会显示为 #VALUE。
Public Function PercentileDetectorYear(ByVal iYear As Integer, ByVal iMonth As Integer, ByVal isWeekend As Variant, ByVal iHour As Integer, _
ByVal dataCol As Integer, ByVal SrchTable As Range, ByVal thisPercentile As Double) As Variant
'scans a table, looking for specific month, isWeekend, and iHour values in specified columns of table.
'Builds an array of contents meeting that criteria, then performs a Percentile computation on that dataset.
Dim monCol As Integer
Dim dayOfWeekCol As Integer
Dim hourCol As Integer
Dim iCt As Double
Dim iRow As Double
Dim dataObs() As Double
Dim rtn As Variant
Dim yearCol As Integer
yearCol = 1
monCol = 2
dayOfWeekCol = 4
hourCol = 5
If isWeekend = True Then
isWeekend = 1
ElseIf isWeekend = False Then
isWeekend = 0
End If
iCt = 0
iRow = 1
With SrchTable
Do While iRow <= .Rows.Count
If .Cells(iRow, yearCol) = iYear And .Cells(iRow, monCol) = iMonth And .Cells(iRow, dayOfWeekCol).Value = isWeekend And .Cells(iRow, hourCol) = iHour Then
If iCt = 0 Then
ReDim dataObs(0)
dataObs(0) = .Cells(iRow, dataCol)
iCt = iCt + 1
Else
ReDim Preserve dataObs(iCt)
dataObs(iCt) = .Cells(iRow, dataCol)
iCt = iCt + 1
End If
End If
iRow = iRow + 1
Loop
End With
rtn = Application.Percentile(dataObs, thisPercentile)
PercentileDetectorYear = rtn
' Application.Calculation = xlCalculationManual
End Function
提前致谢!