office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請(qǐng)用微信掃碼)

        

Excel vba 選中符閤條件的單元格數(shù)據(jù)

2021-01-25 08:00:00
tmtony8
原創(chuàng)
7985

在Excel工作錶中查找符閤條件的數(shù)據(jù)。我們可以用條件格式。設(shè)置相關(guān)的條件,可以以不衕格式突齣顯示符閤條件的數(shù)據(jù)

也可以用vba代碼,判斷範(fàn)圍內(nèi)數(shù)據(jù)是否符閤條件,符閤條件的被選中。

如圖,選中所有超過(guò)60的單元格。


詳細(xì)代碼如下:

Sub SelectByValue()
    Dim Cell As Object
    Dim FoundCells As Range
    Dim WorkRange As Range
    
    If TypeName(Selection) <> "Range" Then Exit Sub
    
    If Selection.CountLarge = 1 Then
        Set WorkRange = ActiveSheet.UsedRange
    Else
       Set WorkRange = Application.Intersect(Selection, ActiveSheet.UsedRange)
    End If
    
    On Error Resume Next
    Set WorkRange = WorkRange.SpecialCells(xlConstants, xlNumbers)
    If WorkRange Is Nothing Then Exit Sub
    On Error GoTo 0
    
    For Each Cell In WorkRange
        If Cell.Value >60   Then
            If FoundCells Is Nothing Then
                Set FoundCells = Cell
            Else
                Set FoundCells = Application.Union(FoundCells, Cell)
            End If
        End If
    Next Cell


    If FoundCells Is Nothing Then
        MsgBox "沒(méi)有記録"
    Else
        FoundCells.Select
        MsgBox "找到 " & FoundCells.Count & " 箇數(shù)據(jù)"
    End If
End Sub

    分享
    文章分類
    聯(lián)繫我們
    聯(lián)繫人: 王先生
    Email: 18449932@qq.com
    QQ: 18449932
    微博: officecn01
    移動(dòng)訪問(wèn)