简体   繁体   中英

Copy data and part of row to another sheet based on date

So I have an Excel workbook consisting of 2 sheets. Sheet2 contains a lot of info

Column A : name
Column B : Size
...
Column P : Validity date

Now Validity date has conditional formatting on to filter on date. Every date before the present day is Red The date of today and 10 days ahead is in Orange Every date after that is Green

In that same workbook is an empty sheet1 for reporting. I would like to have automatically the rows that appear in red on Sheet 2 to be copied to Sheet 1 under a header "Validity date expired" and all the orange ones to appear under another header "About to expire"

How would this be possible?

The following two lines in a macro will copy the formatting and set the value (formulas will recalculate based on the new position, so you need the value) of the original. For this example, I copied the format and value of Sheet1:B2 into Sheet1:D4.

ThisWorkbook.Sheets("Sheet1").Cells(2, 2).Copy ThisWorkbook.Sheets("Sheet1").Cells(4, 4)
ThisWorkbook.Sheets("Sheet1").Cells(4, 4).Value = ThisWorkbook.Sheets("Sheet1").Cells(2, 2).Value

From here, you have to check each row's validity date value against the current date (using the Date variable in a loop. If the date is expired, copy the row. Keep looping until you get to a blank line.

Next, do the same thing, but check the validity date for values about to expire.

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