使用VBA如何基于在另一列中查找匹配值来格式化单元格?

时间:2016-11-02 17:24:29

标签: excel vba excel-vba

我试图有条件地格式化列中的单元格,以便应用以下规则;

  • 如果" Y"在列Z中输入,列D,E和Z中的单元格改变格式。否则保持当前格式。但是,如果" Y"在列Z 中输入单元格D中的输入值与AA列中找到的任何值匹配,应保留当前格式。

以下是与我尝试做的相关的代码 - 它只是整个代码的一部分,因为还有一堆其他的条件格式,我对此很有信心我是如何工作的,它只是使用了我无法掌握的.Find功能的最后一部分。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rCell As Range
Dim tCell As Range
Dim CurrentCellColour As Long
Dim CurrentFontColour As Long
Dim QINSyNo As Long
Dim ReRunNo As Range

For Each tCell In Target
  Select Case tCell.Row
  Case 6 To 1025

  Application.ScreenUpdating = False

    'reset format of each row before applying conditional formatting
   For Each rCell In Range("T" & tCell.Row)

    Range("A" & rCell.Row & ":AB" & rCell.Row).Interior.Color = xlNone
    Range("A" & rCell.Row & ":AB" & rCell.Row).Font.Color = vbBlack

   Next rCell

    'a whole bunch of other conditional formatting code in here...

   For Each rCell In Range("Z" & tCell.Row)
           CurrentCellColour = Range("Z" & rCell.Row).Interior.Color
           CurrentFontColour = Range("Z" & rCell.Row).Font.Color
           QINSyNo = Range("D" & rCell.Row).Value

      Set ReRunNo = Range("AA6:AA1025").Find(What:=QINSyNo, _
                                             LookIn:=xlValues, _
                                             LookAt:=xlWhole, _
                                             SearchOrder:=xlByRows, _
                                             SearchDirection:=xlNext, _
                                             MatchCase:=False, _
                                             SearchFormat:=False)

      If (rCell.Value = "Y" Or rCell.Value = "y") And (ReRunNo Is Nothing) Then
          Range("D" & rCell.Row & ":E" & rCell.Row).Interior.Color = RGB(255, 235, 156)
          Range("D" & rCell.Row & ":E" & rCell.Row).Font.Color = RGB(156, 101, 0)
          Range("Z" & rCell.Row).Interior.Color = RGB(255, 235, 156)
          Range("Z" & rCell.Row).Font.Color = RGB(156, 101, 0)

      ElseIf (rCell.Value = "Y" Or rCell.Value = "y") And (Not ReRunNo Is Nothing) Then
          Range("D" & rCell.Row & ":E" & rCell.Row).Interior.Color = CurrentCellColour
          Range("D" & rCell.Row & ":E" & rCell.Row).Font.Color = CurrentFontColour
          Range("Z" & rCell.Row).Interior.Color = CurrentCellColour
          Range("Z" & rCell.Row).Font.Color = CurrentFontColour

      Else
          Range("D" & rCell.Row & ":E" & rCell.Row).Interior.Color = CurrentCellColour
          Range("D" & rCell.Row & ":E" & rCell.Row).Font.Color = CurrentFontColour
          Range("Z" & rCell.Row).Interior.Color = CurrentCellColour
          Range("Z" & rCell.Row).Font.Color = CurrentFontColour

      End If

   Next rCell

  Application.ScreenUpdating = True

  End Select
Next tCell
End Sub

我得到的结果是,例如,值为" 3"在D:10和" Y"在Z:10中,D,E&的格式化Z变为黄色 - 但原始格式仅在" 3"是AA:10,即" 3"在AA:24仍然改变D,E& Z到黄色。

格式化结果 enter image description here

作为最后一点,我使用vba进行条件格式化的原因是为了防止在复制和粘贴单元格内容时出现错误的情况 - 我试图让用户尽可能做到万无一失。

0 个答案:

没有答案