简体   繁体   English

彩色两格Excel VBA

[英]Color Two Cells Excel VBA

My code currently colors values in Range("N2:N86") anytime I insert a value in that range. 每当我在该范围内插入值时,我的代码当前都会为Range(“ N2:N86”)中的值着色。 However, I want to add an additional line of code that colors or highlights the preceding column Range("M2:M86") whenever a value is entered in Range("N2:N86"). 但是,我想添加一行代码,以便每当在Range(“ N2:N86”)中输入一个值时,就对前一列Range(“ M2:M86”)进行着色或突出显示。

So for example, if i put the value of 1 in N2, I want both N2 and M2 to be highlighted red. 因此,例如,如果我将值1放在N2中,我希望N2和M2都突出显示为红色。 Thanks 谢谢

Dim rCell As Range
Dim inRng As Range
Dim rRng As Range

Set myRng = Range("N2:N86")
myRng.Locked = True

If Range("R4") < 0 Then
    For Each rCell In myRng
        If rCell.Value > 0 Then
            If rRng Is Nothing Then
                Set rRng = rCell
            Else
                Set rRng = Application.Union(rRng, rCell)
            End If
        End If
    Next

    rRng.Locked = False
    rRng.Interior.ColorIndex = 3
End If

I'm not 100% sure on what you are asking for, but here's something that you can test. 对于您要的内容,我不是100%不确定的,但是您可以测试一下。 (Colors rows in both columns upon change in cell value in N column) (在N列中的单元格值更改时,为两列中的行上色)

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Range("N2:N86"), Target) Is Nothing Then
    Target.Interior.ColorIndex = 36
    Target.Offset(, -1).Interior.ColorIndex = 36
End If
Application.EnableEvents = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM