VBA MaxIfs problem with criteria date < cell value

Invisibleman

New Member
Joined
Sep 16, 2015
Messages
18
Dear all,

In VBA I am trying to get the max date value if the date value is lower than the date in another cell. However the result, is skipping a whole month it seems.

As you may see in the source, the last date is March 02, 2025, then the date before is February 27, 2025, which I therefore also want to get in the cell E6, however I am getting the January 27, 2025 as the result.

The Code I am using is;

VBA Code:
Private Sub SetDailyPeriod()
Dim test As Variant
''Call SetNameRange("D4", "E14", "VBA_DailyPeriod")

With New_RAW_PurchaseDetails
    Range("E4") = Application.WorksheetFunction.MinIfs(.Range("E:E"), .Range("F:F"), "Buy") 'Result is okay = 24-2-2024
    Range("E5") = Application.WorksheetFunction.MaxIfs(.Range("E:E"), .Range("F:F"), "Buy") 'Result is okay = 2-3-2025
    Range("E6") = Application.WorksheetFunction.MaxIfs(.Range("E:E"), .Range("F:F"), "Buy", .Range("E:E"), "<" & Range("E5")) 'Result is wrong = 27-1-2025, where it should be 27-2-2025
End With

End Sub

The source of the selected ranges is;
Source.jpg


Which results in;
Result.jpg


If anybody has an idea and can help me that would be very appriciated.

Regards,
Hans
 
Add CLng to convert your date in calc for E6.
Rich (BB code):
    Range("E6") = Application.WorksheetFunction.MaxIfs(.Range("E:E"), .Range("F:F"), "Buy", .Range("E:E"), "<" & CLng(Range("E5")) )
 
Upvote 0
Solution

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