Fluid (moving) sum formula

JayCheezey

New Member
Joined
Jul 25, 2014
Messages
17
Hi everyone!

New coder here, I'm just wondering if I could get some help on some simple code.

I'm trying to create a macro that allows me to continuously get the sum of multiple weeks. Each time I run the macro, it adds one additional week. This is the tricky part as I'm not sure what I function I would use to continuously expand a sum formula.

For example, I run the macro, it sums W1 to W2 (=5). The next time I run the macro, it would sum W1 to W3 (=8). And after that sum W1 to W4 (=10)


ABCDEFG
1W1W2W3
W4W5W6W7
21432645

<tbody>
</tbody>


Thanks a lot in advance. This function is the last part of a large piece of code I'm trying to get done.

Much appreciated! :)
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Why not set things up like this:

ABCDEFGHI
1W1W2W3W4W5W6W76
2143264520

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>

Where I1 contains the week you want to go to, and then use this formula in I2:

Code:
=SUM(A2:INDEX(A2:G2,I1))

Matty
 
Upvote 0
Why not set things up like this:

ABCDEFGHI
1W1W2W3W4W5W6W76
2143264520

<tbody>
</tbody>

Where I1 contains the week you want to go to, and then use this formula in I2:

Code:
=SUM(A2:INDEX(A2:G2,I1))

Matty


Maybe I should give a better description of what I'm trying to accomplish.

Essentially, what I'm trying to do is code in vba.

The situation is I have last years set of numbers. (W1-W52)

As this year progresses, I want to compare this year's W1-W30 with last year's W1-W30.

So next week, when I receive the value for W31, I want the sum for W1-W31 this year, and W1-W31 of last year. I have the coding for this year done. But I'm note entirely sure how to code W1 - W31 of last year.

accomplished this years code by using W1 as a absolute value, and then ".end (xlToRight)" to get the sum from W1-W30 of this year. Unfortunately I can't do this for last years number because the format is like this...

ABCDEFGHIJ
120132013201320132014201420142014Year to DateLast Year to date
2W1W2W3W4W1W2W3W4Sum of 2014sum of 2013
3142567--513

<tbody>
</tbody>


In this example, I'm comparing W1-2 of last year with W1-2 of this year.

Next week, when I get numbers for W3, I want the macro to generate the sum of W1-3 this year and W1-3 last year.

I've got the code for this year...but I can't seem to produce a code to get the sums of last year to move 1 over.


Hope this clears things up!


Thanks!
 
Upvote 0
Why do you need to use VBA when standard formulas will deliver what you need? See below:

ABCDEFGHIJK
120132013201320132014201420142014Year to DateLast Year to dateCurrent Week
2W1W2W3W4W1W2W3W4Sum of 2014sum of 20132
3142567--135

<tbody>
</tbody>

Formulas as follows:

I3:
Code:
=SUM(IF(A1:H1="2014",IF(--SUBSTITUTE(A2:H2,"W","")<=K2,A3:H3)))
J3:
Code:
=SUM(IF(A1:H1="2013",IF(--SUBSTITUTE(A2:H2,"W","")<=K2,A3:H3)))

Both of which need entering with CTRL+SHIFT+ENTER.

Matty
 
Upvote 0
Hi,

In response to your PM...

If you're trying to create some kind of report, I would strongly suggest keeping data and results completely separate. This will then keep things very flexible if you want to carry out additional comparisons at a later stage.

If you need to retain the format you've described and want to pursue the VBA option, reply back with what you're wanting to do and hopefully one of the coders on here will be able to help.

Matty
 
Upvote 0
Thanks for the advice!

Hi,

In response to your PM...

If you're trying to create some kind of report, I would strongly suggest keeping data and results completely separate. This will then keep things very flexible if you want to carry out additional comparisons at a later stage.

If you need to retain the format you've described and want to pursue the VBA option, reply back with what you're wanting to do and hopefully one of the coders on here will be able to help.

Matty
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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