Month to date totals by category

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
918
Office Version
  1. 365
Platform
  1. Windows
The following code runs but it keeps giving me YTD(year to date values.) I don't want that. I want to be able to display
the sum of all category values from the first day of each month. Below is the Sheet named AOS whose category values are in col B, date values in col C
and amounts to be totaled for each category from month to date in col E. Here is the code:
Code:
Sub MTDAOSTOTALSBYCAT()
    Dim DynamicRange As Range
    Dim d1 As Date, d2 As Date, strDATE As Date
    Dim wb As Worksheet, ws As Worksheet ' do you need this line?
    Dim t As Currency ' do as above?
    Dim a, i As Long, x, n As Long
    'new code
    d1 = Date
    d1 = DateSerial(Year(d1), _
     Month(d1), 1)
    MsgBox d1
    d2 = Date
    NewReport.DTPicker1.value = d1
    NewReport.DTPicker2.value = d2
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    a = Sheets("AOS").Range("A1").CurrentRegion.value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For i = 1 To UBound(a, 1)
            If a(i, 3) >= d1 And a(i, 3) <= d2 Then
              .Item(a(i, 2)) = .Item(a(i, 2)) + a(i, 5)
            End If
        Next
        x = Array(.keys, .Items): n = .count
    End With
        If n > 0 Then
       Sheets("REPORT").Range("A2").Resize(n, 2).value = _
       Application.Transpose(x)
    Else
        MsgBox "No data in date range"
    End If
    Worksheets("REPORT").Select   'new code to total values
        Range("B2").Select
        Set DynamicRange = Range(Selection, Selection.End(xlDown))
        DynamicRange.Name = "mytotal"
        Range("mytotal").End(xlDown).Select
        ActiveCell.Offset(2, 0).Select
        ActiveCell.value = "=SUM(mytotal)"
         ActiveCell.Select
         Selection.NumberFormat = "$#,##0.00"
        ActiveCell.Offset(0, -1).Select
        ActiveCell.value = "Totals"
         Application.EnableEvents = True
        Application.ScreenUpdating = True
        AOSTOTBYCAT.Show
        AOSTOTBYCAT.TextBox1.value = ActiveCell.value
        AOSTOTBYCAT.TextBox1.Text = Format(AOSTOTBYCAT.TextBox1.Text, "$ #,##0.00")
        AOSTOTBYCAT.Caption = "MTD AOS Totals By Category"
        Sheets("BUDGET").Select
End Sub

ENTIRE RANGE TO DATE.png

I wrote this code and can't fiugre out why its still giving me YTD category totals instead of MTD category totals.
Any help is greatly appreciated. BTW I prefer not to use an advanced autofilter if possible.
I'm not yet up to speed in understanding whether using the =SUMIFS formula can do this or not. The result is displayed in a userform with the total values of each
MTD category in a oolumn to the right of each category.

Thanks for any help. , cr
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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