简体   繁体   English

如果单元格在Excel中为红色/绿色/黄色,则复制并粘贴整行

[英]Copy paste entire row if the cell has red/green/Yellow color in excel

I have a table with values based on monthly based and product in 1st column as shown below (Say "sheet1"). 我在第一列中有一个表格,其中包含基于月度值和产品的值,如下所示(说“ sheet1”)。 This table has conditional formatting applied to highlight cell colors red/yellow/green based on target. 此表已应用条件格式,以根据目标突出显示红色/黄色/绿色的单元格颜色。 So, I want to take only specific month(current month- May) column and want VBA to check, if any cell in that column is Red, select that entire row and copy past it into another sheet for example "Sheet2" with headers. 因此,我只想选择特定的月份(当前月份-五月)列,并希望VBA检查该列中是否有红色单元格,请选择整行并将其复制到另一个带有标题的表中,例如“ Sheet2”。

Product   =  Target   Feb Mar Apr May
Wood     >=    5      10  10  10  10
wood     >=    5      28  28  28  28
Tree     >=   12      30  45  60  68
plastic  >=   45      50  50  50  50
tree     >=   50      50  50  50  50
iron     >=   100     64  75  75  80

The values will change monthly and even sometimes target, so the coloring can be totally green/yellow/red sometimes. 这些值将每月更改一次,有时甚至是目标值,因此有时着色可能完全是绿色/黄色/红色。 So, I want to automate this process using VBA, to go and check on "Sheet1" every month and pull any rows which has red cell highlighted for that month. 因此,我想使用VBA自动化该过程,每月检查“ Sheet1”并提取该月红色单元格突出显示的任何行。 OR even a button function could also help, so I can assign this macro to a button, this action should be done by clicking the button. 甚至按钮功能也可能有帮助,因此我可以将此宏分配给按钮,此操作应通过单击按钮来完成。 Even it should also clear all rows from earlier month in Sheet2, which might have highlighted in last month but not in this month. 即使是它也应该清除Sheet2上一个月前的所有行,这些行可能在上个月已突出显示,但在本月未突出显示。

I tried, different ways to do this, its not recognizing the cell color. 我尝试了不同的方法来执行此操作,因为它无法识别单元格颜色。 When I use the random/regular cell out of table and fill with red color and use find(ctrl+f) popup box to highlight using color format, it is working but its not taking the cells which has conditional formatting in a table. 当我使用表格中的随机/常规单元格并用红色填充并使用find(ctrl + f)弹出框使用颜色格式突出显示时,它可以工作,但不能使用表格中具有条件格式的单元格。 So, I don't know if there is a way to identify all the cells in the table which is highlighted in red using VBA. 因此,我不知道是否有一种方法可以识别使用VBA以红色突出显示的表中的所有单元格。

INPUT (IN WORKSHEET1): 输入(在工作表1中):

MYIMAGE

OUTPUT (IN WORKSHEET2): 输出(在工作表2中):

MYIMAGE

    Sub newnew()

declaring the worksheets, Sheet2 and Sheet3 should be changed to match your sheet names 声明工作表,Sheet2和Sheet3应该更改为与工作表名称匹配

    Dim ws As Worksheet
        Set ws = Sheets("Sheet2")

    Dim wsTwo As Worksheet
       Set wsTwo = Sheets("Sheet3")

clearing out previous data in wsTwo 清除wsTwo中的先前数据

    wsTwo.Cells.Clear

finding the last row and column for ws with data inside 查找ws的最后一行和最后一列,其中包含数据

    Dim lastRow As Integer
        lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    Dim lastColumn As Integer
        lastColumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

we are setting up the header here 我们在这里设置标题

    ws.Range("A1:F1").Copy
    wsTwo.Range("A1:F1").PasteSpecial xlPasteAll
    Application.CutCopyMode = False

beginning our loop from the last row and moving upwards till we hit row 2. We are checking if rowNum and lastColumn display format interior color is red. 从最后一行开始循环,然后向上移动直到达到第2行。我们正在检查rowNum和lastColumn显示格式内部的颜色是否为红色。 If it is we are taking that range, copying it, then pasting it in wsTwo. 如果是这样,我们将复制该范围,然后将其粘贴到wsTwo中。 Note that we have that rowCounter which we set before entering the for loop. 请注意,在进入for循环之前,我们已经设置了rowCounter。 we will use that as a placeholder, incrementing every time that we paste in ws two, so that we don't paste over data we just pasted. 我们将其用作占位符,每次粘贴两次ws时都会递增,这样就不会粘贴刚刚粘贴的数据。

     Dim rowCounterWsTwo As Integer
         rowCounterWsTwo = 2

    For rowNum = lastRow To 2 Step -1
            If Cells(rowNum, lastColumn).DisplayFormat.Interior.Color = vbRed = vbRed Then
                ws.Range(Cells(rowNum, 1), Cells(rowNum, lastColumn)).Copy
                wsTwo.Range("A2:F2").Insert
                Application.CutCopyMode = False
                rowCounterWsTwo = rowCounterWsTwo + 1
            End If
    Next rowNum

End Sub

暂无
暂无

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

相关问题 如果单元格的颜色为绿色,则从 Sheet1 复制一行并将其粘贴到 Sheet 2 - Copy a row from Sheet1 and paste it into Sheet 2 if color of a cell is green 复制整个Excel行并粘贴到表格底部 - Copy entire excel row and paste to the bottom of the table Excel VBA 自动根据单元格值复制整行“X”次并粘贴到单独的工作表中 - Excel VBA automation copy entire row “X” times based on cell value and paste in separate sheet Excel宏:如果列B具有“ X”,则复制整行并粘贴到名为“列B”的工作表中 - Macro for Excel: If Column B has “X”, then copy entire row and paste in Worksheet named “Column B” 复制并粘贴整行 - Copy and Paste Entire Row 查找具有列最大值的单元格,复制整个行并粘贴到第2行 - Find cell with max value of column, copy entire row and paste in row 2 复制并粘贴在黄色突出显示的行下方 - Copy and paste below the yellow highlighted row 如果单元格具有背景色,请删除整行 - Delete entire row, if a cell has a background color VBA 如果单元格与 ID 列表匹配,则复制并粘贴整行,但如果列表包含空白单元格或带有“”的单元格,则不要粘贴 - VBA Copy and paste entire row if cell matches list of IDs, but do not paste if list contains blank cell or cell with "" 如果列中的单元格有数据,则复制并粘贴到同一行中的不同单元格 - If cell in column has data then copy and paste to a different cell in same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM