Modify code to look at previous month

RodneyW

Active Member
Joined
Sep 24, 2010
Messages
431
Office Version
  1. 2013
Platform
  1. Windows
I'm using this code:

=SUMPRODUCT(--('D:\Corporate\Analytics\2020Analytics\[Members2020.xlsx]Apr'!$A$3:$A$200=$B$1),'D:\Corporate\Analytics\2020Analytics\[Members2020.xlsx]Apr'!$D$3:$D$200)

which pulls information based on the tab named APR. There is a similar tab for every month. The APR tab is not populated until May, May isn't populated until June and so on. I need to modify the code above so that instead of looking at a specific predetermined tab, it looks at what the current calendar month is it will pull data from the previous month. To give it an example, right now it is September. The formula should pull from the AUG tab.

How do I do this?

Thank you in advance
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
That is possible, but will need to use the INDIRECT function which is slow, volatile & doesn't work on closed workbooks.
Will that be ok?
 
Upvote 0
Fluff, I was thinking simple ranges and math. Maybe I missed something . . .

Rather than depending on checking the calendar since you appear to be populating worksheets, I'd use a target "range" based on date; i.e. a month. The target range can be manually created, or created using Visual Basic. I think either way I'd create all the worksheets and give them month names. You can hide the ones not in use if needed. If you want to create the ranges by hand, i.e on the August worksheet one range could be $A$3:$A$500 , I'd give them names easy to manage like "r_AUG_ & "whatever the column is" Because your ranges have simple names the formula is easily changed to "r_SEP_ & "whatever the column is" etc. The tallys can be in row A for a quick sanity check and then all together on a summary worksheet.

How comfortable are you with named ranges or Visual Basic and how automatic do you need it to be?

Hope I didn't miss read what you need.

Ron
 
Upvote 0
FLUFF --- it might work, I'm willing to explore it.
SHISEIJI --- I'm not familiar at all with Visual Basic but am somewhat familiar and comfortable with ranges.
 
Upvote 0
How about
Excel Formula:
=SUMPRODUCT(--(INDIRECT("'D:\Corporate\Analytics\2020Analytics\[Members2020.xlsx]"&TEXT(EDATE(TODAY(),-1),"mmm")&"'!$A$3:$A$200")=$B$1),INDIRECT("'D:\Corporate\Analytics\2020Analytics\[Members2020.xlsx]"&TEXT(EDATE(TODAY(),-1),"mmm")&"'!$D$3:$D$200"))
 
Upvote 0
Looks like Fluff has a formula for you, but I think you will need to adapt it for each worksheet or else increase the ranges on a single worksheet and just add records to the end as they come in. My KISS approach would be to just make all the ranges and then use each one as needed as you are already using worksheets for each month. Paste in the data and the formulas are already set up. But that's only because I'm really comfortable with doing things that way.

Ron
 
Upvote 0
That formula will always look at the previous months sheet, no need to change anything. ;)
 
Upvote 0
I learned something too, didn't know indirect would look at the whole workbook. Never used it.

Ron
 
Upvote 0
Thanks guys. This gives me enough to work with for now. Lots of time sensitive projects right now. I'll dive in deeper on Monday. Thanks again for the help
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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