I am having problems with calculating numerator and denominator with excel VBA. The num and den are limited to the quarter in which data was entered in the excel spreadsheet.
Here is an example:
I have two columns in my excel sheet:
Date_data_entered_for each_person Number of visits to the office
1/1/2009 5
1/10/2009 6
2/10/2009 7
5/12/2009 9
Based on this example, I have to calculate “average number of visits” per quarter = sum of visits/number of persons. In this example for first quarter of 2009, the data was entered for 3 persons and the total number of visits were, 5+6+7 = 18, hence the average = 18/3 = 6.
The code I have written is:
Function Visits(target_timeframe)
Target_Quarter = Format(target_timeframe, "YYYYq")
For vRow = 2 To 2000
entered_timeframe = Format(Sheets("sheet1").Range("A" & vRow).Value, "YYYYq")
Entered_Visits = Sheets("sheet1").Range("B" & vRow).Value 'equals value of column
If (entered_timeframe = Target_Quarter) Then
denominator = denominator + 1
If (Entered_Visits > 0) Then
numerator = numerator + 1
End If
End If
Next
If denominator > 0 Then
Average = numerator / denominator
Else
Average = "N/A"
End If
End Function
I am not able to figure out what is wrong with code. Its not calculating the average. I would be extremely grateful if somebody could help me with this issue.
Thanks for you time!
I think that instead of:
You want:
With the current code, you’re simply adding 1 to the numerator each time, when in reality you want to be adding the number of visits to the numerator.