【问题标题】:Copy Range From One Sheet Paste Part of Range In another Sheet Based On Cell colorindex根据单元格颜色索引从一张表复制范围将部分范围粘贴到另一张表中
【发布时间】:2019-01-24 18:44:29
【问题描述】:

我正在尝试复制一个工作表上的一系列单元格并根据颜色索引将颜色粘贴到另一个工作表上。

我想复制 sheet1 上的单元格

并且只在 sheet2 上粘贴 colorindex = 49 的单元格

这是我尝试过的: 有没有比编写 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

【问题讨论】:

标签: excel vba range background-color copy-paste


【解决方案1】:

您可以使用此 sn-p 将内部颜色复制到第二张纸上。如果您想指定另一个已经存在的“第二个”工作表,您可以将工作表名称改为 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

【讨论】:

  • 这会将所有值复制到一行。需要对应同一个cell
【解决方案2】:

试试这个功能

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

然后你可以在 if 语句中使用它。如果 getfillcolor(cell) = 49 然后做点什么

【讨论】:

    猜你喜欢
    • 2022-10-08
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2022-08-23
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多