【问题标题】:Is there a way to conditionally format cells if they are linked to by a formula in another cell?如果单元格通过另一个单元格中的公式链接到单元格,有没有办法有条件地格式化单元格?
【发布时间】:2019-05-25 03:00:47
【问题描述】:

在第一张表上,我在 A 列中有描述,在 B 列中有值。在第二张表上,我在 A 到 F 列中有公式,我根据描述中的内容手动填充了第一张表 B 列中的值A列是。如果有问题的单元格已在第二张表格的公式中使用,我想突出显示表格第一列 B 中的单元格。尽可能使用条件格式,否则使用宏。

我希望能够快速查看 B 列中的单元格是否已添加到另一个工作表上的公式中,这样我就不会不小心将值包含两次。

  Sheet One               Sheet Two
     A     B     C           A     B     C
1  salt    3            1       =B1+B4
2  base    3            2
3  base    4            3
4  salt    1            4             =B2+B3
5  base    4            5

我希望能够自动突出显示已经在另一个函数中的单元格,而无需手动执行以减少错误机会。在上面的示例中,单元格 B1 到 B4 将突出显示,因为它们已在公式中使用,而 B5 将保持正常,因为它尚未使用

【问题讨论】:

  • 您使用的是什么电子表格应用程序? (来自评论)。
  • 我更喜欢谷歌表格,这样我可以在一天中添加行项目,但我可以做笔记并在每天结束时输入 Excel 表格。这就是我目前使用下面的代码所做的。
  • 考虑到您使用的是 VBA 答案,那么应该删除 google-sheets,如果您想要 Google 表格的解决方案,您应该发布一个新问题。这是因为每个应用程序使用不同的编程语言,并且几个电子表格函数的工作方式不同。
  • 我知道他们每个人都使用不同的语言。我留下标签的想法是,如果 google-sheets 方法得到回答,它会使答案不那么分散。允许其他人同时看到两者并在它们之间进行选择。我理解这是否只是降低了搜索功能的效率,因为没有人回答过谷歌表格的答案。

标签: excel vba formatting conditional-statements


【解决方案1】:

VBA 条件格式功能。数组和范围联合

特点

  • Find Method (SO)
  • 单元格属性
  • 父属性
  • 公式属性
  • 调整大小方法
  • InStr 函数
  • 替换函数
  • 联合方法

代码

Sub CompareColumnWithRange()

  Const cStrTgtWs As Variant = 1        ' Target Worksheet Name/Index
  Const cStrSrcWs As Variant = 2        ' Source Worksheet Name/Index
  Const cLngTgtFirst As Long = 1        ' Target First Row
  Const cLngSrcFirst As Long = 1        ' Source First Row
  Const cStrTgtColumn As Variant = "B"  ' Target Column Letter/Number
  Const cStrSrcRange As String = "A:F"  ' Source Columns Range
  Const cColor As Long = 255            ' Formatting Color

  Dim rngTgt As Range                   ' Target Range
  Dim rngU As Range                     ' Target Union Range
  Dim vntSrc As Variant                 ' Source Array
  Dim i As Long                         ' Source Array Row Counter
  Dim j As Integer                      ' Source Array Column Counter
  Dim k As Long                         ' Target Range Row Counter

  With Worksheets(cStrSrcWs).Range(cStrSrcRange)
    ' Check if sheet is empty (No data).
    If .Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
        Is Nothing Then Exit Sub
    ' Paste Source Range's formulas into Source Array. Since the previous
    ' With statement refers to a range, the Parent property has to be used
    ' to 'aquire sheet level'.
    vntSrc = .Parent.Range(.Parent.Cells(cLngSrcFirst, .Column), _
        .Parent.Cells(.Cells.Find("*", , , , , 2).Row, _
        .Columns.Count - .Column + 1)).Formula
  End With

'  ' Print contents of vntSrc to Immediate window.
'  For i = 1 To UBound(vntSrc)
'    For j = 1 To UBound(vntSrc, 2)
'      Debug.Print vntSrc(i, j)
'    Next
'  Next

  ' Target Column vs Source Array
  With Worksheets(cStrTgtWs)
    ' Determine the Target Range (1 column).
    Set rngTgt = .Cells(cLngTgtFirst, cStrTgtColumn).Resize( _
        .Cells(.Rows.Count, cStrTgtColumn).End(xlUp).Row - cLngTgtFirst + 1)
    ' Loop through Target Range (1 column)
    For k = cLngTgtFirst To .Cells(.Rows.Count, cStrTgtColumn).End(xlUp).Row
      ' Loop through Source Array rows.
      For i = 1 To UBound(vntSrc)
        ' Loop through Source Array columns.
        For j = 1 To UBound(vntSrc, 2)
          ' Search for Target Range's cell address in current value
          ' of Source Array i.e. remove the $ signs in both, and add
          ' sheet name for Target Range.
          If InStr(1, Replace(vntSrc(i, j), "$", ""), .Name & "!" _
              & Replace(.Cells(k, cStrTgtColumn).Address, "$", "")) <> 0 Then
            If Not rngU Is Nothing Then ' Add cells to existing range.
              Set rngU = Union(rngU, .Cells(k, cStrTgtColumn))
             Else ' Add cells to non-existing range. Runs only the first time.
              Set rngU = .Cells(k, cStrTgtColumn)
            End If
            Exit For  ' If a value has been found, stop searching for more.
          End If
        Next
      Next
    Next
  End With

  ' Apply formatting to all 'collected' cells in Target Union Range in one go.
  If Not rngU Is Nothing Then
    rngU.Interior.Color = cColor
    Set rngU = Nothing
  End If

  Set rngTgt = Nothing

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多