简体   繁体   中英

Sumif and countif criteria isn't working in Excel VBA

I am writing an excel vba code for expected shortfall evaluation.

My sumif and countif functions return value 0 if I put in criteria ' "<" & Var ' (value at risk - percentile of array at 2,5%), if I reverse that criteria (">") these functions sum/count all values in range.

I can't find the mistake, can anyone help? Here is a part of the code:

Dim RiskHorizon As Integer
RiskHorizon = InputBox("Set the Risk Horizon!", "Risk Horizon", 10)
Dim SigLev As Double
SigLev = InputBox("Set the Significance Level!", "Significance Level", 0.025)
Dim CountDates As Integer
CountDates = Application.CountIf(Sheets("ExpSh").Columns("A"), "<>" & "") - 1
Dim ReturnRange As Range
Set ReturnRange = Sheets("ExpSh").Range("C2:C" & CountDates - RiskHorizon + 1)
Dim Var As Double
Dim ES As Double
Dim Sums As Double
Dim Counts As Double
Var = Application.WorksheetFunction.Percentile(ReturnRange, SigLev)
Sums = Application.WorksheetFunction.SumIf(ReturnRange, "<" & Var)
Counts = Application.WorksheetFunction.CountIf(ReturnRange, "<" & Var)
ES = Sums / Counts

Perhaps:

Sums = Application.WorksheetFunction.SumIf(ReturnRange, ""<"" & Var)
Counts = Application.WorksheetFunction.CountIf(ReturnRange, ""<"" & Var)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM