【发布时间】:2020-02-03 18:17:09
【问题描述】:
我正在搜索具有特定字体属性的第一个单元格的范围。 我已经查看了类似问题的其他解决方案并按照他们的示例(例如Searching for Bold formatted Cells),但查找未找到具有给定字体属性的单元格 - 在即时窗口中,我已确认该范围内有一个单元格具有正在搜索的 TintAndShade 和 ThemeColor 值。
我们将不胜感激。
Public Const nThemeColor = xlThemeColorAccent6
Public Const sTintAndShade = -0.249977111117893
Public Function FindFontAttributesRow(rng As Range)
'finds row number of first cell with specific font attributes in a columnar range
Dim FirstAddress As String, Cell As Range
Application.FindFormat.Clear
With Application.FindFormat.Font
.themeColor = nThemeColor
.TintAndShade = sTintAndShade
End With
Set Cell = rng.Find(What:="*", SearchFormat:=True)
If Not Cell Is Nothing Then
FindFontAttributesRow = Cell.Row
Else
MsgBox "ERROR: dbg"
Stop
End If
End Function
我已经缩小了问题的范围(我认为)。在下面的代码中,当 SearchFormat=True 时会出现错误 91(“Object variable or with block variable not set”),但当它=False 时不会出现,这似乎表明 FindFormat 对象没有被正确引用(但我可以'不知道如何解决它!)。
Sub Macro12()
Dim cel As Range
Columns("A:A").Select
Range("A21").Activate
Application.FindFormat.Clear
With Application.FindFormat.Font
.themeColor = 10
End With
Range("A21").Activate
ThisWorkbook.ActiveSheet.Columns("A:A").Find(What:="*", SearchFormat:=True).Activate
End Sub
我相信我找到了答案:我添加了“After”参数(设置为 rng(1,1))并且它有效。这与 MS 文档相反,该文档说如果缺少 After arg,则默认为范围内的左上角单元格。
【问题讨论】:
-
你没有定义
nThemeColor或sTintAndShade? -
您是否要遍历单元格并找到主题颜色和色调?你根本没有循环。