got wrong answer Countifs VBA Excel with condition between 2 date

andrianocaras

New Member
Joined
Jan 25, 2016
Messages
4
ursmV.png


i need a help with my VBA excel . . . i have an excel like the picture above and i was trying countifs with condition between 2 date
in counting i got the right answer 5 but in counting1 i got 8 instead the right answer 5 in counting2 i got 0 instead the right answer 5 or wrong answer 8 just like in counting1, in counting2 i just reverse the condition this is the code

HTML:
Dim rB As Range
MyDate = CDate(Range("B6"))
MyDate1 = CDate(Range("B2"))
Set rB = Range("B2", Range("B" & Rows.Count).End(xlUp))
LS = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
counting = Application.WorksheetFunction.CountIfs(rB, ">=" & MyDate1, rB, "<=" & MyDate)
counting1 = Application.WorksheetFunction.CountIfs(rB.Offset(0, 1), "<=" & MyDate, rB, ">=" & MyDate1)
counting2 = Application.WorksheetFunction.CountIfs(rB.Offset(0, 1), ">=" & MyDate1, rB, "<=" & MyDate)
MsgBox counting
MsgBox counting1    
MsgBox counting2

counting = i want to count B with condition >=Mydate1 and <=MyDate i got answer 5
counting1 = i want to count C with condition <=Mydate and >=MyDate1 i got answer 8
counting2 = i want to count C with condition >=Mydate1 and <=MyDate i just reverse the condition, i got answer 0
can anyone help me so i can get the right answer ??
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

I am not sure what you're trying to acomplish with these countings and your logic is somewhat off.
your VBA part
Code:
[COLOR=#000080]Application.WorksheetFunction.CountIfs(rB.Offset(0, 1), "<=" & MyDate, rB, ">[/COLOR]="  & MyDate1)
is creating this formula
Code:
=COUNTIFS(C2:C12,"<="&MyDate,B2:B12,">="&MyDate1)
so it checks a dateserialnumber (in this case MyDate) against a set of numbers. As the date serial number for MyDate (B6 = 4-may-11) = 40667 any number in column C will is smaller than 40667 combined with the criteria 2 being the date in column B being greater than B2; this formual will evaluate to 8.

Counting2 will only come to result if your number in column C are greater than 40571 (being the dateserial for B2) combined with a date that's older then 4-may-11

Than solving your issue comes with questions and assumptions first:
1. counting1 - You want to count C with condition <=Mydate and >=MyDate1 and then? eg. Do you need the actual cell filed with numbers or .....
2. counting2 - i assume You want to count all numbers outside of the date selection

try this:
Code:
Dim rB As Range
MyDate = Range("B6").Value
MyDate1 = Range("B2").Value
Set rB = Range("B2", Range("B" & Rows.Count).End(xlUp))
LS = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
counting = Application.WorksheetFunction.CountIfs(rB, ">=" & MyDate1, rB, "<=" & MyDate)
counting1 = Application.WorksheetFunction.CountIfs(rB, ">=" & MyDate1, rB, "<=" & MyDate, rB.Offset(0, 1), ">=0")
counting2 = Application.WorksheetFunction.Count(rB.Offset(0, 1)) - counting1
MsgBox counting
MsgBox counting1
MsgBox counting2
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,539
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top