简体   繁体   English

根据单元格colorindex从一个工作表的粘贴范围复制范围到另一工作表的另一工作表

[英]Copy Range From One Sheet Paste Part of Range In another Sheet Based On Cell colorindex

I am trying to copy a range of cells on one worksheet and paste the color on another worksheet based on the colorindex. 我正在尝试在一个工​​作表上复制一系列单元格,然后根据colorindex将颜色粘贴到另一工作表上。

I want to copy cells on sheet1 我想复制sheet1上的单元格

IMG1

and only paste cells with colorindex = 49 on sheet2 并且仅将sheet2上的colorindex = 49的单元格粘贴

IMG2

This is what I've tried doing: Is there a better or faster way of doing this than writing 90 If statements? 这就是我尝试做的事情:是否有比编写90 If语句更好或更快速的方法?

Private Sub CommandButton3_Click()

If Range("A1").Interior.ColorIndex = 49 Then
Worksheets("Sheet2").Range("A1").Interior.ColorIndex = 49
Else: Range("A1").Interior.ColorIndex = -4142
End If

If Range("A2").Interior.ColorIndex = 49 Then
Worksheets("Sheet2").Range("A2").Interior.ColorIndex = 49
Else: Range("A2").Interior.ColorIndex = -4142
End If

If Range("A3").Interior.ColorIndex = 49 Then
Worksheets("Sheet2").Range("A3").Interior.ColorIndex = 49
Else: Range("A3").Interior.ColorIndex = -4142
End If

If Range("A4").Interior.ColorIndex = 49 Then
Worksheets("Sheet2").Range("A4").Interior.ColorIndex = 49
Else: Range("A4").Interior.ColorIndex = -4142
End If

If Range("A5").Interior.ColorIndex = 49 Then
Worksheets("Sheet2").Range("A5").Interior.ColorIndex = 49
Else: Range("A5").Interior.ColorIndex = -4142
End If

End Sub

Try this function 试试这个功能

Function GetFillColor(Rng As Range) As Long
      GetFillColor = Rng.Interior.ColorIndex
End Function

Then you can use it in an if statement. 然后,可以在if语句中使用它。 If getfillcolor(cell) = 49 then do something 如果getfillcolor(cell)= 49,那么做点什么

You can use this snippet to copy the interior color over to the second sheet. 您可以使用此代码段将内部颜色复制到第二张纸上。 If you want to specify another 'second' sheet that already exists you can put the sheet name like this instead Sheets("Sheet Name").Interior ... . 如果要指定另一个已经存在的“第二”工作表,则可以这样放置工作表名称,而不是Sheets("Sheet Name").Interior ...

If sheets.count < 2 Then sheets.Add after:=sheets(1)

Dim theCell As Range
For Each theCell In sheets(1).Range("A1:E16")
    With theCell
        If .Interior.ColorIndex = 49 Then
            sheets(2).Cells(.row, .Column).Interior.ColorIndex = 49
        Else
            sheets(2).Cells(.row, .Column).Interior.ColorIndex = -4142
        End If
    End With
Next theCell

暂无
暂无

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

相关问题 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 将范围从一张纸复制/粘贴到另一张纸 - Copy/Paste Range from one sheet to another 尝试从一张纸上复制一个范围并将其粘贴到另一张纸上一列中的下一个空单元格 - Trying to copy a range from one sheet and paste it to the next empty cell in a column on another sheet Excel宏复制单元格范围并将数据粘贴到另一个工作表 - Excel Macro Copy cell range & paste data one sheet to another 将选定的范围从一个 xls 文件粘贴到另一个指定的工作表和单元格 - “范围 class 的复制方法失败” - Paste selected range from one xls file to another into a designated sheet and cell - “Copy method of Range class failed” 从范围循环中的每个单元格复制数据并将其粘贴到另一张纸上 - copy data from each cell in range loop and paste it on another sheet Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match 从工作表复制范围,然后粘贴到不同列的另一工作表中 - Copy range from a sheet and paste into another sheet in different columns 从Sheet1复制范围并将其粘贴到Sheet 2中 - Copy Range from Sheet1 And paste it in Sheet 2 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM