使用EXCEL查找阴影单元格,如果找到则对其他单元格进行阴影处理

问题描述 投票:0回答:1

使用 EXCEL 引用单元格的颜色,并根据该颜色对第二个工作表进行更改。

总体思路是查找一个单元格是否在一张工作表(sheet1)的列(B:B)内有阴影。如果发现阴影,请从工作表 2 的 B 列中阴影单元格的行中查找 f 列的值。突出显示该行。

我完全不知道如何开始。

excel vba conditional-statements macros formula
1个回答
0
投票
Option Explicit
Sub Demo()
    Dim oSht1 As Worksheet, oSht2 As Worksheet
    Dim ColB1 As Range, ColB2 As Range, c As Range, ce As Range
    Dim vValue
    Const HL_COLOR = vbYellow ' highlight color
    Set oSht1 = Sheets("Sheet1") ' update sheet name as needed
    Set oSht2 = Sheets("Sheet2")
    Set ColB1 = Application.Intersect(oSht1.UsedRange, oSht1.Columns(2))
    Set ColB2 = Application.Intersect(oSht2.UsedRange, oSht2.Columns(2))
    ' remove highlight on Sheet2
    oSht2.Cells.Interior.Color = xlNone
    For Each c In ColB1.Cells
        If c.Interior.Color <> vbWhite Then
            vValue = oSht1.Cells(c.Row, "F").Value
            Set ce = ColB2.Find(vValue, LookIn:=xlValues, lookat:=xlWhole)
            If Not ce Is Nothing Then
                ce.EntireRow.Interior.Color = HL_COLOR
            End If
            Set ce = Nothing
        End If
    Next
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.