简体   繁体   中英

(VBA) Extract and copy and paste column values for specified date range for corresponding row values

I've been stuck on this for a while now. What I need is the title - what I have is:

Sheet 1:
1 billion mostly unnecessary columns.

Column D: Values I need         Column F:         Column M:
Revenue                         Names             Date

12                              John              1/24/2016  2:40:02 AM
15                              Sarah             3/2/2016   4:35:17 PM
14                              Sarah             7/17/2016  1:50:10 PM
20                              Matt              8/20/2012  4:16:12 AM
10                              John              11/19/2015 5:04:05 AM
etc.                            etc.

Current Sheet 2: Pivot Table*

Row Label:                  
Sarah     
Matt
John
etc.

Desired Sheet 2 Pivot Table+*

Row Label:             Column __:  
                       Revenue

Sarah                  29
Matt                   0 *(note: see below, but = 0 because Matts value corresponded to date 2012)
John                   22
etc.

The important thing about Sheet 2 is that I would like to tell VBA to find the nearest emptiest column within the sheet, and input the values from Column D from S1 (and sum for duplicates) but only sum and extract values that range from today's date to 11 months prior. Currently I made a module to automatically create a pivot table for the next sheet, but I have wanted to add the above for a while, just stuck.

I'm also assuming I won't have to specify sum if I tell VBA to extract data for corresponding row label names? And I was thinking about putting a Begin Date and End Date cells on Sheet 2 to refer to, or using the TODAY() function somehow for the date, but not sure how to specify to extract and sum values for the date range from today to 11 months prior.

Edit: *Please note this is a watered down version of my current situation, everything is bigger so to avoid confusion, pivots are necessary, but I choose to dilute it for the sake of this issue.

Do you really need a pivot table?

If you dont, just create a new sheet with all the names, set a cell for the starting date you want to consider and do =sumifs

For example

On Sheet2 B1 you put 1/24/2016. Then on A2 you insert the name (Sarah, for example) On B2 you put the formula

=Sumifs(Sheet1!D:D,Sheet1!F:F,A2,Sheet1!M:M,">"&B$1)

After that just make a list of name and drag the formula beside every name.

*My excel is in portuguese so maybe the formula needs some other ajustments to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM