首页 > 解决方案 > 删除带有条件格式为红色的单元格的列

问题描述

我计划对第 18 行中的单元格进行条件格式设置,然后清除带有红色单元格的列。

当我用红色填充单元格时,此代码会删除列,但在使用条件格式时不会。

Sub sbDelete_Columns_Based_On_Cell_Color()
    Dim lColumn As Long
    Dim iCntr As Long

    lColumn = 50
    For iCntr = lColumn To 1 Step -1
        If Cells(18, iCntr).Interior.Color = Excel.XlRgbColor.rgbRed Then
            Columns(iCntr).Delete
        End If
    Next iCntr
End Sub

标签: excelvba

解决方案


Using the below code you can scrape cell color from conditional formatting:

Option Explicit

Sub test()

    Dim Color As Long

    With ThisWorkbook.Worksheets("Sheet2")
    
        Color = .Range("D1").DisplayFormat.Interior.Color
    
    End With
    
End Sub

推荐阅读