If 语句不适用于 ActiveCell.Font.Size = 20 Then

If Statement Not working With ActiveCell.Font.Size = 20 Then

我正在尝试从我拥有的一长串数据中抓取一些信息。我需要从中抓取的每个段的开头(我需要激活的活动单元格)的字体大小为 20。但是,这个循环是 运行 并且工作但是当我需要退出的单元格时循环,它再次进入 else(形成无限循环)。下面是我的代码:

Dim repeat As Boolean
repeat = True    

Do While repeat = True
   If ActiveCell.Font.Size = 20 Then
      repeat = False
   Else
      ActiveCell.End(xlDown).Activate
   End If
Loop

我希望程序在 activecell 的字体大小为 20 时退出循环。

如果您将 Range.FindSearchFormat 一起使用,它可能看起来像这样:

Sub Macro1()
    Application.FindFormat.Clear
    Application.FindFormat.Font.Size = 20

    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim foundCell As Range
    Set foundCell = ws.Range("A:A").Find(What:="", _
                                         After:=ws.Cells(ws.Rows.Count, 1), _
                                         LookIn:=xlFormulas, _
                                         LookAt:=xlPart, _
                                         SearchDirection:=xlNext, _
                                         MatchCase:=False, _
                                         SearchFormat:=True)

    If Not foundCell Is Nothing Then
        Debug.Print foundCell.Address '<- foundCell is the one you want
    End If

End Sub

如果你想要一个常规的 For 循环:

Dim ws as Worksheet
Set ws = ActiveSheet

Dim lastRow as Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

Dim i as Long
For i = 1 to lastRow
    If ws.Cells(i, 1).Font.Size = 20 Then 
        Exit For '<- Cells(i, 1) is the one you want
    End IF
Next i