excel - 仅根据格式查找单元格

标签 excel vba

这个问题在这里已经有了答案:





Excel VBA, getting range from an inactive sheet

(3 个回答)


5个月前关闭。




我正在尝试遍历工作表并查找字体大小为 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

最佳答案

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

关于excel - 仅根据格式查找单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70934814/

相关文章:

vba - 如何在 VBA 中查找并取消隐藏隐藏模块

用于将一个 excel 的某些列转换为新 excel 的 python 脚本

vba - 如何强制用户在工作表中启用宏?

excel - 选择范围内包含空格的单元格

excel - 从一个文件中随机复制数据,然后使用宏将其随机粘贴到其他 Excel 中

sql - 如何使用 ADODB 在一次执行中返回多个记录集?

windows - 如何检查 excel 工作簿当前是否在 MAC 或 Windows 上工作

string - 如何在 Excel 中将字符串转换为数字,反之亦然

vba - 在 Excel 图表上切换分组轴

vba - Excel VBA : Delete row if Column A contains a number within the string