简体   繁体   English

根据日期将数据和行的一部分复制到另一张纸上

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

So I have an Excel workbook consisting of 2 sheets. 因此,我有一个包含2张纸的Excel工作簿。 Sheet2 contains a lot of info Sheet2包含很多信息

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 今天之前的每个日期都是红色今天和未来10天的日期都是橙色

In that same workbook is an empty sheet1 for reporting. 在同一工作簿中有一个空的sheet1用于报告。 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" 我想自动将工作表2中红色显示的行复制到标题“有效日期已过期”下的工作表1中,并将所有橙色行复制到另一个标题“即将过期”下

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. 对于此示例,我将Sheet1:B2的格式和值复制到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. 在这里,您必须对照当前日期检查每行的有效日期值(在循环中使用Date变量。如果日期已过期,请复制该行。继续循环直到得到空白行。

Next, do the same thing, but check the validity date for values about to expire. 接下来,做同样的事情,但是检查有效日期是否即将过期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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