Loop through Dates for a value

zach454

New Member
Joined
Apr 25, 2011
Messages
28
I am having a problem I can't figure out. I have every day in a date format in Column B which I'm wanting to ONLY search for the current month and grab the values in Column F and add them, giving me a monthly total. I have this loop which only works if the 1st row is this current month. I can't see to figure this out? Any suggestions? Thanks!

c = Month(Date)

For Each c In DateRng
j = Range("F" & k).Offset(i, 0)
i = i + 1
sum = sum + j
Debug.Print sum
If Month(c) <> Month(Date) Then Exit For
Next c
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'm not sure what your macro is doing being that it is just part of the whole Macro.

You could accomplish the same without a Macro by using a Formula. Look at the table below, I put the formula in to Sum 2 columns in case you may(or want) the same.

Excel Workbook
ABCDEF
1Month to SearchMarTotals$345.00$2,445.00
2DATEValue1Value2
32/9/201122190
42/9/2011183378
52/9/2011454249
63/1/201166200
73/30/20111172083
83/30/2011162162
94/5/2011236684
104/5/201158237
114/15/201123114
124/25/2011841327
134/25/20111911635
Sheet1
Excel 2007
Cell Formulas
RangeFormula
E1=SUMPRODUCT(--(TEXT($B$3:$B$30,"mmm")=$C$1),E$3:E$30)
F1=SUMPRODUCT(--(TEXT($B$3:$B$30,"mmm")=$C$1),F$3:F$30)


By changeing C1 to the month (i.e Feb or Apr) it will Sum based off of that month.
 
Last edited:
Upvote 0
You're stopping as soon as you find a date which is the current month. You need to complete the loop and only add cells which match the current month.

Maybe:-
Code:
c = Month(Date) [COLOR=green]' this isn't doing anything as you are overwriting c immediately[/COLOR]

For Each c In DateRng
   If Month(c) = Month(Date) Then 
      j = Range("F" & k).Offset(i, 0)
      i = i + 1
      sum = sum + j
      Debug.Print sum
   Endif
Next c
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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