首页 > 解决方案 > 搜索具有特定字体属性的第一个单元格的范围

问题描述

我正在搜索具有特定字体属性的第一个单元格的范围。我已经查看了类似问题的其他解决方案并遵循了他们的示例(例如Searching for Bold formatted Cells),但是 find 没有找到具有给定字体属性的单元格 - 在即时窗口中,我确认有一个单元格在具有正在搜索的 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,则默认为范围内的左上角单元格。

标签: excelvbafind

解决方案


我添加了 'After' 参数(设置为 rng(1,1))并且它有效。这与 MS 文档相反,该文档说如果缺少 After arg,则默认为范围内的左上角单元格。


推荐阅读