【问题标题】:Find Cell Based only on Format [duplicate]仅根据格式查找单元格[重复]
【发布时间】:2022-02-01 10:28:15
【问题描述】:

我正在尝试遍历工作表并查找字体大小为 22 的单元格。这是我目前所拥有的,但是我不断返回第一个找到的术语的值,因为它遍历床单。我假设我需要在某个地方合并一个 FindNext?

Sub FindFormatCell()

Dim ws As Worksheet
Dim rngFound As Range

With Application.FindFormat.Font
        .Size = 22
End With

For Each ws In Worksheets
    If ws.Tab.ColorIndex = 20 And ws.Visible = xlSheetVisible Then
        Set rngFound = Cells.Find(What:="", SearchFormat:=True)
        Debug.Print rngFound
    End If
Next ws
        
Application.FindFormat.Clear

End Sub

【问题讨论】:

  • Cells.Find() 将默认为活动表,因此您需要使用ws.Cells.Find()。是的,您确实需要在循环中运行 Find(),并存储找到的第一个单元格的地址,这样您就知道 Find() 何时循环回来。例如参见stackoverflow.com/a/55349092/478884(您需要修改它以添加您的FindFormat 条件)

标签: excel vba


【解决方案1】:

Find 方法 - SearchFormat 搜索

Option Explicit

Sub FindFormatCell()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Application.FindFormat.Font.Size = 22
    
    Dim ws As Worksheet
    Dim sCell As Range
    Dim srg As Range
    Dim urg As Range
    Dim FirstAddress As String
    
    For Each ws In wb.Worksheets
        If ws.Visible = xlSheetVisible And ws.Tab.ColorIndex = 20 Then
            Set srg = ws.UsedRange
            ' "" - empty cells, "*" - occupied cells
            Set sCell = srg.Find(What:="", _
                After:=srg.Cells(srg.Rows.Count, srg.Columns.Count), _
                LookIn:=xlValues, SearchOrder:=xlByRows, SearchFormat:=True)
            If Not sCell Is Nothing Then
                FirstAddress = sCell.Address
                Do
                    If urg Is Nothing Then
                        Set urg = sCell
                    Else
                        Set urg = Union(urg, sCell)
                    End If
                    Set sCell = srg.FindNext(sCell)
                Loop Until sCell.Address = FirstAddress
            End If
            If Not urg Is Nothing Then
                ' Start doing stuff here.
                
                Debug.Print ws.Name, urg.Address(0, 0)
                            
                ' End doing stuff here.
                Set urg = Nothing
            End If
        End If
    Next ws
            
    Application.FindFormat.Clear

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多