VBA-Excel find and select multiple cells

后端 未结 2 943
天涯浪人
天涯浪人 2020-12-11 10:00

i\'m writing a code and i\'m stuck on this problem which i think should not bee too difficult to solve but i don\'t manage it.

I need my program to find all cells w

2条回答
  •  时光说笑
    2020-12-11 11:05

    Use the Union method to collect the ranges into one discontiguous range then .Select them before leaving the sub

    Sub FindAll()
        Dim firstAddress As String, c As Range, rALL As Range
        With Worksheets(4).Range("a1:l500")
            Set c = .Find("myValue", LookIn:=xlValues)
            If Not c Is Nothing Then
                Set rALL = c
                firstAddress = c.Address
                Do
                    Set rALL = Union(rALL, c)
                    Worksheets(4).Range(c.Address).Activate
                    Set c = .FindNext(c)
    
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If
            .Activate
            If Not rALL Is Nothing Then rALL.Select
        End With
    End Sub
    

提交回复
热议问题