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

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

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

微信交流群(請用微信掃碼)

        

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

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

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

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

如圖,選中所有超過60的單元格。


詳細代碼如下:

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 "沒有記錄"
    Else
        FoundCells.Select
        MsgBox "找到 " & FoundCells.Count & " 個數(shù)據(jù)"
    End If
End Sub

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