【问题标题】:Script to move cell values according to bg colour根据背景颜色移动单元格值的脚本
【发布时间】:2015-02-02 04:46:51
【问题描述】:

下面的代码是我目前所拥有的。但由于某种原因,它说我有一个没有 for 的下一个 soureRow。任何帮助都会很棒。我试图让这个脚本循环遍历第 4 到第 10 页,并且如果该行的背景颜色为黄色或红色,并且第一个没有匹配的值。将该行复制到工作表 1 的底部。

target = "Sheet1"

For allSheets = 4 To 10

lastTargetRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row

Sheets(allSheets).Activate
lastCurrentRow = Sheets(allSheets).Range("A" & Rows.Count).End(xlUp).Row
For sourceRow = 2 To lastCurrentRow
    If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Yellow Then

        For checkRow = 2 To lastTargetRow
            If ActiveSheet.Cells(sourceRow, "B").Value <> Sheets(target).Cells(checkRow, "B").Value Then
            nRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row + 1
            For lCol = 1 To 26       'Copy entire row by looping through 6 columns
                Sheets(target).Cells(nRow, lCol).Value = Sheets(allSheets).Cells(sourceRow, lCol).Value
            Next lCol
            End If

        Next checkRow

    If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Red Then

        For checkRow2 = 2 To lastTargetRow
            If ActiveSheet.Cells(sourceRow, "B").Value <> Sheets(target).Cells(checkRow, "B").Value Then
            nRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row + 1

            For lCol = 1 To 26       'Copy entire row by looping through 6 columns
                Sheets(target).Cells(nRow, lCol).Value = Sheets(allSheets).Cells(sourceRow, lCol).Value
            Next lCol

            End If

        Next checkRow2

        End If

Next sourceRow
Next allSheets

【问题讨论】:

  • 您在以If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Yellow Then 开头的块上缺少End If

标签: excel vba for-loop copying


【解决方案1】:

这可能会让你更接近:

Sub Tester()

    Const TARGET As String = "Sheet1"

    Dim shtTarget As Worksheet, allSheets As Long, nextTargetRow As Long
    Dim shtTmp As Worksheet, lastCurrentRow As Long, sourceRow As Long
    Dim clr As Long, f As Range, bCell As Range

    Dim myYellow As Long            '<<EDIT
    myYellow = RGB(255, 235, 156)

    Set shtTarget = Sheets(TARGET)
    nextTargetRow = shtTarget.Cells(Rows.Count, "A").End(xlUp).Row + 1

    For allSheets = 4 To 10

        Set shtTmp = Sheets(allSheets)
        lastCurrentRow = shtTmp.Cells(Rows.Count, "A").End(xlUp).Row

        For sourceRow = 2 To lastCurrentRow
            Set bCell = shtTmp.Cells(sourceRow, "B")
            clr = bCell.Interior.Color 'get the color
            'is yellow or red?
            If clr = myYellow  Or clr = vbRed Then
                'look in colB on Target sheet for the value from source
                Set f = shtTarget.Columns(2).Find(bCell.Value, lookat:=xlWhole)
                If f Is Nothing Then
                   'ColB value is not already listed 
                   shtTarget.Cells(nextTargetRow, 1).Resize(1, 26).Value = _
                            shtTmp.Cells(sourceRow, 1).Resize(1, 26).Value
                    nextTargetRow = nextTargetRow + 1
                End If
            End If
        Next sourceRow

    Next allSheets

End Sub

【讨论】:

  • 这很棒,但我可以将黄色定义为 RGB(255, 235, 156)
  • 谢谢,这很完美,但它似乎只适用于红色
  • 选择“红色”单元格并在 VB 编辑器立即窗格中键入 ? Selection.Interior.Color 并按 Enter。您看到的值是否与输入 ? RGB(255, 235, 156) 时相同?
  • 没关系,我只是一个菜鸟,它的工作非常感谢,如果我想添加复制颜色格式会很困难,或者我只是选择整个工作表并分配颜色。
  • 其实我只是通过添加这个代码得到它 shtTarget.Cells(nextTargetRow, 1).Resize(1, 26).Interior.Color = clrm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2016-02-08
  • 2014-09-30
  • 1970-01-01
  • 2017-12-27
相关资源
最近更新 更多