【问题标题】:Delete columns with cells with the conditional formatting red删除带有条件格式为红色的单元格的列
【发布时间】:2019-07-09 21:59:27
【问题描述】:

我计划对第 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

【问题讨论】:

  • Cells(18, iCntr).DISPLAYFORMAT.Interior.Color
  • 您的条件格式规则正在使用某种逻辑来突出显示红色单元格,因此只需在此处使用完全相同的逻辑。您正在使用条件格式作为中间人。跳过中间人,在 VBA 中编写你的逻辑

标签: excel vba


【解决方案1】:

使用以下代码,您可以从条件格式中刮取单元格颜色:

Option Explicit

Sub test()

    Dim Color As Long

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多