【问题标题】:VBA MACRO to color ONE cell right to the Selected cellsVBA MACRO为选定单元格右侧的一个单元格着色
【发布时间】:2021-02-20 20:15:29
【问题描述】:

我有一个宏,它制定了多个单元格区域的格式规则,如果它包含“S”并且它可以工作,则必须将单元格涂成黄色。但我也想要包含“S”的单元格右侧的单元格涂成黄色,但右侧只有一个单元格 - 不是整行,这可能吗?我想这将发生在“WITH 语句中,但我无法继续前进

Sub Makro2()
    Range("D6:E30,G6:H30,J6:K30,M6:N30,P6:Q30").Select
    Range("P6").Activate
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""S"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

【问题讨论】:

  • 您想为“P6”单元格或中断范围设置格式规则吗?由于中断范围有两列区域,因此第一个区域的 E:E 列已经是条件格式。我的意思是,“在 D:D 右侧的单元格上......这个想法是格式条件只对它所属的单元格起作用。但是可以调整相同的代码以同时格式化右侧的下一个单元格。如果有兴趣,我可以告诉你如何为一个单元格完成它。理论上,它可以只对每个不连续范围的第一个右列完成,但要复杂一些。
  • 为了知道是否是这种情况,如果我能找到一些时间来解决更复杂的不连续范围,我想知道您需要格式化的行为方式。例如,如果“D10”中存在“S”,但“E10”中没有“S”具有自己的格式,您是否希望“E10”也被突出显示(黄色内部)?
  • 对不起,我没有得到正确的解释,当我记录宏并标记区域时,P6 只是最后一个活动单元格。我只是在问题中插入了一张图片以便更好地理解。在我写的时候,我的条件格式适用于我的区域,但我只希望格式化单元格右侧的单元格也被绘制
  • 我明白你说的,但我不明白你的意思...我在之前的评论中问了一个问题,关于“E10” cel如果它没有类似“S”的值,但“D10”有,则行为。它的内部是否应该是黄色的?我应该再问一遍吗?我的意思不是很清楚吗?
  • 那么,您的图片在 E、H、K 列中没有显示任何“S”……您的中断范围是否构建错误?您是否错过了向我们展示这种情况?您的要求涉及我上面提到的列也可以在“S”的情况下制作黄色内部......

标签: excel vba conditional-formatting


【解决方案1】:

我尝试在评论中解释尝试对不连续范围的条件格式的参与。对于真正由您显示的代码处理的单元格,您可以使用下一个代码完成您需要的工作。条件格式化行为的基础是它只格式化条件格式所属的单元格

Sub Makro2Bis()
    Dim rng As Range, offrng As Range
    Set rng = Range("P6"): Set offrng = rng.Offset(0, 1)
    With rng
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
        .FormatConditions(1).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
    With offrng
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Address(0, 0) & "= ""S"""
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With
End Sub

请进行测试并发送一些反馈。

已编辑

请测试(更复杂的)版本,以您要求的方式为中断范围创建条件格式:右边的相邻单元格也将具有内部黄色:

Sub Makro3Bis()
 Dim rng As Range, arr, rng1 As Range, rng2 As Range, rng3 As Range
 
 Set rng = Range("D6:E30,G6:H30,J6:K30,M6:N30,P6:Q30")
 arr = buildThreeRngs(rng)
 Set rng1 = arr(0) 'the first column of the discontinuous range areas
 Set rng2 = arr(1) 'the second column of the discontinuous range areas
 Set rng3 = arr(2) 'the next column after the discontinuous range areas
 
 With rng1
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
    .FormatConditions(1).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
 End With
 With rng2 'it will have two conditions. The second one relative to its left neighbour cell.
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
    .FormatConditions(1).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng1.cells(1).Address(0, 0) & "= ""S"""
    .FormatConditions(2).Interior.Color = rng1.FormatConditions(1).Interior.Color
    .FormatConditions(2).StopIfTrue = False
 End With
 With rng3
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng2.cells(1).Address(0, 0) & "= ""S"""
    .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
    .FormatConditions(1).StopIfTrue = False
 End With
End Sub

请进行测试并发送一些反馈。

二次编辑

只看你的图片而不是基于你发布的代码,可能我的第一个代码(用于一个单元格)以下一种方式改编应该是你需要的:

Sub Makro2BisBis()
    Dim rng As Range, offrng As Range
    Set rng = Range("D6:D30,G6:G30,J6:J30,M6:M30,P6:P30")
    Set offrng = rng.Offset(0, 1)
    Debug.Print offrng.Address: Stop
    With rng
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
        .FormatConditions(1).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
    With offrng
        .FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.cells(1).Address(0, 0) & "= ""S"""
        .FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
        .FormatConditions(1).StopIfTrue = False
    End With
End Sub

它将使您在代码中的范围内的单元格变为黄色,但仅使用每个区域的第一列...

请进行测试并发送一些反馈。

【讨论】:

    【解决方案2】:

    VBA 中的条件格式

    • 对于Excel 的第二部分,您可以使用以下Conditional Formatting 公式:

      =D6="s"
      

    守则

    Option Explicit
    
    Sub Makro2()
        
        Const ColOffset As Long = 1
        Const Criteria As String = "s"
        
        Dim rg As Range: Set rg = Range("D6:D30,G6:G30,J6:J30,M6:M30,P6:P30")
        
        ' xlCellValue
        With rg
            .ClearFormats
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
                Formula1:="=""" & Criteria & """"
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 65535
                    .TintAndShade = 0
                End With
                .StopIfTrue = False
            End With
        End With
        
        ' xlExpression
        With rg.Offset(, ColOffset)
            .ClearFormats
            .FormatConditions.Add Type:=xlExpression, _
                Formula1:="=" & .Cells(1).Offset(, -ColOffset).Address(0, 0) _
                    & "=""" & Criteria & """"
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 65535
                    .TintAndShade = 0
                End With
                .StopIfTrue = False
            End With
        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
      • 2015-10-11
      • 2013-04-09
      相关资源
      最近更新 更多