Wrong date for sumif on userform

tuine

New Member
Joined
Oct 12, 2018
Messages
9
Hi everyone,
I got three columns for my spreadsheet.
column A is date in format of dd/mm/yyyy
column B is Customer name
column C is total sale for each customer.

I set up a userform to check total of sale for a customer from a certain date. for example I want to know how many items have been delivered to customer John from 04/11/2018.

My trouble is the code below not work properly. It did not pick up the correct date.
TextBox1 is where I enter the date. 4/11/2018
ComboBox1 is where I enter the customer name John

Could you please help to fix my code. Many thanks

Sub Sum_ifs()


Dim NumOfItem, Customer, DateRange As Range
Set NumOfItem = Range("C:C")
Set Customer = Range("B:B")
Set DateRange = Range("A:A")
S_date = UserForm1.TextBox1.Value


MsgBox WorksheetFunction.SumIfs(NumOfItem, Customer, UserForm1.ComboBox1, DateRange, ">=" & CDate(S_date))
End Sub
 
Alternatively, you can achieve the same without using the helper column, but you'd need to modify your VBA code and skip SumIfs function:

Code:
Sub Sum_ifs()
[COLOR=#ff0000]    Dim NumOfItem, rngCell As Range[/COLOR]
    Dim S_date As Date
[COLOR=#ff0000]    Dim lngLastRow As Long[/COLOR]
[COLOR=#ff0000]    Dim lngResult As Long[/COLOR]
    
[COLOR=#ff0000]    lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row[/COLOR]

[COLOR=#ff0000]    Set NumOfItem = Range("C2:C" & lngLastRow)[/COLOR]
    
    S_date = UserForm1.TextBox1.Value
    
[COLOR=#ff0000]    For[/COLOR][COLOR=#ff0000] Each rngCell In NumOfItem[/COLOR]
[COLOR=#ff0000]        If IsNumeric(rngCell.Value) _[/COLOR]
[COLOR=#ff0000]            And rngCell.Offset(0, -1).Value = "John" _[/COLOR]
[COLOR=#ff0000]            And rngCell.Offset(0, -2).Value >= CLng(S_date) _[/COLOR]
[COLOR=#ff0000]            And rngCell.Font.ColorIndex = 3 Then[/COLOR]
[COLOR=#ff0000]                lngResult = lngResult + rngCell.Value[/COLOR]
[COLOR=#ff0000]        End If[/COLOR]
[COLOR=#ff0000]    Next rngCell[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]    MsgBox lngResult[/COLOR]


End Sub
 
Last edited:
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
JustynaMK, you are super, thanks very very much. Both are very good. As I have no idea how to do the first one, I am thinking about the second one and you give the solution right away. You are very smart.
??
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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