以红色突出显示所有具有混合引用公式的单元格

问题描述 投票:0回答:1

我目前拥有的 VBA 代码还将所有具有绝对引用的单元格绘制为红色(例如:

=$A$1+A$2
),这就是它不满足我目前需要的地方。 我只需要其中至少一个公式具有混合引用(例如:
=$A$1+$A2
)或仅混合引用(例如:
=$A1+A$2
)的单元格颜色为红色。

VBA代码:

Sub test2()
Dim r As Range
Dim c As Range

Worksheets("Plan1k").Activate
Set r = ActiveSheet.UsedRange
With r
.Interior.ColorIndex = xlNone
End With
For Each c In r
 If InStr(c.Formula, "$") > 0 Then
  c.Interior.ColorIndex = 3
 End If
Next c
excel excel-2010
1个回答
0
投票

这段代码使用了正则表达式程序集,你必须引用它。

Sub test2()
Dim r As Range
Dim c As Range
  Dim RegEx As RegExp
  Set RegEx = New RegExp
  RegEx.Pattern = "(([^\x24]|^)[a-zA-Z]+\x24\d+)|(\x24[a-zA-Z]+\d+)"
  RegEx.IgnoreCase = True
  RegEx.Global = False
Worksheets("Plan1k").Activate
Set r = ActiveSheet.UsedRange
With r
.Interior.ColorIndex = xlNone
End With
For Each c In r
  Set res = RegEx.Execute(c.Formula)
  If res.Count > 0 Then c.Interior.ColorIndex = 3
  'If InStr(c.Formula, "$") > 0 Then
  
  'End If
Next c
End Sub

样品测试结果:

© www.soinside.com 2019 - 2024. All rights reserved.