Sumif in VBA

Enzo_Matrix

Board Regular
Joined
Jan 9, 2018
Messages
113
I have created this code to help with our production schedule but the values are not returning what I would expect them to.
Code:
Sub ProductCount()'Create 3 headings of Overdue,Today,Next Week and sum data next to each heading


Dim OD As Range
Set OD = Sheets("Weld").Range("B1")
Dim TDY As Range
Set TDY = Sheets("Weld").Range("D1")
Dim TW As Range
Set TW = Sheets("Weld").Range("F1")


Sheets("Weld").Activate
    
    ActiveSheet.Range("A1").Select
        ActiveCell.Value = "Overdue"
    With OD
        .Formula = "=SUMIF($G:$G," > " & TODAY(),$F:$F)"
        .Value = .Value
    End With


    ActiveSheet.Range("C1").Select
        ActiveCell.Value = "Today"
    With TDY
        .Formula = "=Sumif($G:$G,Today(),$F:$F)"
        .Value = .Value
    End With
    
    ActiveSheet.Range("E1").Select
        ActiveCell.Value = "This Week"
    With TW
        .Formula = "=Sumif($G:$G," > " & TODAY(),$F:$F)"
        .Value = .Value
    End With
        
End Sub

What is supposed to happen
A1: "Overdue"
B1: count of product past due
- Actual result is the word 'True'
C1: "Today"
D1: count for product due for completion today
E1: "This Week"
F1: count of product for next week
- Actual result is the word 'True'

If I put this directly into a cell it does generate the correct value
Code:
=Sumif($G:$G," > " & TODAY(),$F:$F)

can someone please help me with my code to give actual values in cells B1, F1.
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Modified line of code
Code:
ActiveSheet.Range("A1").Select        
ActiveCell.Value = "Overdue"
    With OD
        .Value = Application.SumIf("F", " > " & TODAY(), "G")
    End With

I ran the code and received an error:
"Compile error:
Sub or Function not defined"

Please help
 
Last edited:
Upvote 0
Use Date instead of Today()

You can't type a single letter. Try e.g. Range("F:F") but include the rows and columns rather than just single columns (which will contain many empty cells and slow down the calculation)
 
Last edited:
Upvote 0
If I use Date for this instead of TODAY, is there a way to set it so that the date moves with each day of the week?

As I originally mentioned, this will be a daily production schedule that has it's fields updated with each new entry
 
Upvote 0
If I use Date for this instead of TODAY, is there a way to set it so that the date moves with each day of the week?
Date is the VBA equivalent of TODAY(). They work the same way.
The VBA equivalent of the Excel Date function is DateSerial

Confused yet? :confused:
 
Last edited:
Upvote 0
beyond belief.
This is what my code now looks like and I am getting some values, but not all.
B1 should have a total of overdue orders, it shows '0'
F1 should show orders for next week, it shows '0'
D1 correctly shows orders due today at '49'

Code:
Sub Production()'set fields of count as well as hose count for each category


Dim OD As Range
Set OD = Sheets("Weld").Range("B1")
Dim TDY As Range
Set TDY = Sheets("Weld").Range("D1")
Dim TW As Range
Set TW = Sheets("Weld").Range("F1")


Sheets("Weld").Activate
    
    ActiveSheet.Range("A1").Select
        ActiveCell.Value = "Overdue"
    With OD
        Cells(1, 2) = Application.WorksheetFunction.SumIf(Range("G:G"), " > " & Date, Range("F:F"))
    End With


    ActiveSheet.Range("C1").Select
        ActiveCell.Value = "Today"
    With TDY
        .Formula = "=Sumif((G:G),Today(),(F:F))"
        .Value = .Value
    End With
    
    ActiveSheet.Range("E1").Select
        ActiveCell.Value = "This Week"
    With TW
        Cells(1, 6) = Application.WorksheetFunction.SumIf(Range("G:G"), " < " & Date, Range("F:F"))
    End With
        
End Sub

Am I missing something?
 
Last edited:
Upvote 0
Try

Code:
With OD
        .Formula = "=SUMIF($G:$G,[COLOR=#ff0000]""[/COLOR]>[COLOR=#ff0000]""[/COLOR] & TODAY(),$F:$F)"
        .Value = .Value
End With

M.
 
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,160
Members
449,209
Latest member
BakerSteve

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