【问题标题】:Copy entire row from current sheet to another sheet on color basis根据颜色将整行从当前工作表复制到另一张工作表
【发布时间】:2018-10-29 22:44:14
【问题描述】:

我当前的工作表有数据,其中很少有绿色的单元格,我需要将那些单元格有绿色的行(只有几个绿色的单元格)移动或复制到另一个工作表。我已经为此编写了代码,但是循环在每一行的第一列上运行,但不会检查该行中的每个单元格。我需要检查每个单元格的每一行,如果有任何绿色单元格,那么它应该将整行复制并粘贴到下一行的另一张表中

Sub Copy()

lastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

sheet2Counter = 1

For i = 1 To lastRow

ConditionalColor = Worksheets("Sheet1").Cells(i, 1).Interior.ColorIndex

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A" & i & " ").Select

If ConditionalColor = 35 Then
ActiveCell.EntireRow.copy
Worksheets("Sheet2").Activate

lastrow1 = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
If Not Worksheets("Sheet2").Range("A" & lastrow1 & " ") = "" And Not i = 1 Then
lastrow1 = lastrow1 + 1
Worksheets("Sheet2").Range("A" & lastrow1 & " ").Select

    With Selection
        .PasteSpecial Paste:=xlPasteAll
    End With
Else
Worksheets("Sheet2").Range("A1").Select

    With Selection
        .PasteSpecial Paste:=xlPasteAll
    End With

End If

Worksheets("Sheet1").Cells(i, 1).Value

End If

Next

End Sub

【问题讨论】:

  • 这些单元格是否用条件格式着色?因为.Cells(i, 1).Interior.ColorIndex检测条件格式。您可以使用 DisplayFormat ,即 .Cells(i, 1).DisplayFormat.Interior.ColorIndex
  • 但这会检测到我需要的颜色..根据我的要求,我需要 35 个索引颜色并且效果很好..但这里的问题是我能够为第一列中的每一行执行此操作,但是不是该特定行中的每个单元格。如果每行的任何单元格的颜色索引为 35,我想复制整行并粘贴到另一张表中
  • 好的,那么您没有使用条件格式 - 从名称 ConditionalColor 中不清楚。
  • 是的,我没有使用条件格式,因为我的公式很差......所以我从来没有尝试过。但编写了几行 VBA 代码,适用于每一行的第一列。
  • 使用 ColorIndex 是个坏主意,因为它可能会根据您使用的主题而改变。请改用 RGB 值。

标签: excel vba


【解决方案1】:

你可以这样做:

Option Explicit

Sub CopyByColor()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim lastRowSrc As Long, nextRowDest As Long, i As Long

    Set shtSrc = Worksheets("Sheet1")
    Set shtDest = Worksheets("Sheet2")

    lastRowSrc = shtSrc.Cells(Rows.Count, 1).End(xlUp).Row
    nextRowDest = shtDest.Cells(Rows.Count, 1).End(xlUp).Row + 1

    For i = 1 To lastRowSrc
        'only check used cells in the row...
        If IsColorMatch(Application.Intersect(shtSrc.Rows(i), shtSrc.UsedRange)) Then
            shtSrc.Rows(i).Copy shtDest.Cells(nextRowDest, 1)
            nextRowDest = nextRowDest + 1
        End If
    Next i

End Sub

Function IsColorMatch(rng As Range)
    Const INDEX_COLOR As Long = 35
    Const INDEX_COLOR_BAD As Long = 3 'or whatever...
    Dim c As Range, indx

    IsColorMatch = False '<< default

    For Each c In rng.Cells
        indx = c.Interior.ColorIndex
        If indx = INDEX_COLOR Then
            IsColorMatch = True
        Elseif indx = INDEX_COLOR_BAD Then
            IsColorMatch = False
            Exit Function '<< got a "bad" color match, so exit
        End If
    Next c

End Function

编辑:使用“查找格式”方法的IsColorMatch 的不同实现:

Function IsColorMatch(rng As Range) As Boolean
    If RangeHasColorIndex(Selection.EntireRow, 6) Then
        IsColorMatch = Not RangeHasColorIndex(Selection.EntireRow, 3)
    Else
        IsColorMatch = False
    End If
End Function

Function RangeHasColorIndex(rng As Range, indx As Long)
    With Application.FindFormat
        .Clear
        .Interior.ColorIndex = indx
    End With
    RangeHasColorIndex = Not rng.Find("", , , , , , , , True) Is Nothing
End Function

【讨论】:

  • 感谢您的代码..但需要做一些小改动......我在某些单元格中有红色以及同一行中的颜色索引 35..我不想要包含的行红色和颜色索引 35 .. 我只需要包含单元格颜色索引 35 的行
  • 太棒了..您编辑和更新的代码运行良好。谢谢你
猜你喜欢
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多