[英]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上的单元格
and only paste cells with colorindex = 49 on sheet2 并且仅将sheet2上的colorindex = 49的单元格粘贴
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.