Answers for "Find Next or Find All"

0

Find Next or Find All

'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
     
    'This procedure:
        '(1) Finds all cells whose value is 10 in cells A6 to H30 of the "Find Next All" worksheet in this workbook
        '(2) Sets the found cells' interior/fill color to light green
     
    'Declare variable to hold/represent searched value
    Dim MyValue As Long
     
    'Declare variable to hold/represent address of first cell where searched value is found
    Dim FirstFoundCellAddress As String
     
    'Declare object variable to hold/represent cell range where search takes place
    Dim MyRange As Range
     
    'Declare object variable to hold/represent cell where searched value is found
    Dim FoundCell As Range
     
    'Specify searched value
    MyValue = 10
     
    'Identify cell range where search takes place
    Set MyRange = ThisWorkbook.Worksheets("Find Next All").Range("A6:H30")
     
    'Find first cell where searched value is found
    With MyRange
        Set FoundCell = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    End With
     
    'Test whether searched value is found in cell range where search takes place
    If Not FoundCell Is Nothing Then
         
        'Store address of first cell where searched value is found
        FirstFoundCellAddress = FoundCell.Address
         
        Do
             
            'Set interior/fill color of applicable cell where searched value is found to light green
            FoundCell.Interior.Color = RGB(63, 189, 133)
             
            'Find next cell where searched value is found
            Set FoundCell = MyRange.FindNext(After:=FoundCell)
         
        'Loop until address of current cell where searched value is found is equal to address of first cell where searched value was found
        Loop Until FoundCell.Address = FirstFoundCellAddress
     
    End If
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Sub FindNextAll()
    'Source: https://powerspreadsheets.com/
    'For further information: https://powerspreadsheets.com/excel-vba-find/
     
    'This procedure:
        '(1) Finds all cells whose value is 10 in cells A6 to H30 of the "Find Next All" worksheet in this workbook
        '(2) Sets the found cells' interior/fill color to light green
     
    'Declare variable to hold/represent searched value
    Dim MyValue As Long
     
    'Declare variable to hold/represent address of first cell where searched value is found
    Dim FirstFoundCellAddress As String
     
    'Declare object variable to hold/represent cell range where search takes place
    Dim MyRange As Range
     
    'Declare object variable to hold/represent cell where searched value is found
    Dim FoundCell As Range
     
    'Specify searched value
    MyValue = 10
     
    'Identify cell range where search takes place
    Set MyRange = ThisWorkbook.Worksheets("Find Next All").Range("A6:H30")
     
    'Find first cell where searched value is found
    With MyRange
        Set FoundCell = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    End With
     
    'Test whether searched value is found in cell range where search takes place
    If Not FoundCell Is Nothing Then
         
        'Store address of first cell where searched value is found
        FirstFoundCellAddress = FoundCell.Address
         
        Do
             
            'Set interior/fill color of applicable cell where searched value is found to light green
            FoundCell.Interior.Color = RGB(63, 189, 133)
             
            'Find next cell where searched value is found
            Set FoundCell = MyRange.FindNext(After:=FoundCell)
         
        'Loop until address of current cell where searched value is found is equal to address of first cell where searched value was found
        Loop Until FoundCell.Address = FirstFoundCellAddress
     
    End If
     
End Sub
Posted by: Guest on October-12-2021

Browse Popular Code Answers by Language