+ Reply to Thread
Results 1 to 5 of 5

VBA find on hit go to next row

Hybrid View

GOR4N VBA find on hit go to next row 05-01-2020, 11:58 AM
xladept Re: VBA find on hit go to... 05-01-2020, 02:17 PM
GOR4N Re: VBA find on hit go to... 05-01-2020, 02:35 PM
mehmetcik Re: VBA find on hit go to... 05-01-2020, 02:37 PM
xladept Re: VBA find on hit go to... 05-01-2020, 02:50 PM
  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    Belgrade
    MS-Off Ver
    Excel 2010
    Posts
    17

    VBA find on hit go to next row

    I made a simple loop to go trough worksheet and to print multiple hits for .Find in a Range
    Right now my code displays all cells containing any data in it.

    What I would like is:
    when there is a HIT to continue with .FindNext but to skip to next row

    This is my sample table:
    NAME VALUE 1 VALUE 2 VALUE 3
    WORKER 1 somethinW1.1 somethingW1.2
    WORKER 2 somethingW2.2 somethingW2.3
    WORKER 3 somethingW3.1 somethingW3.3
    WORKER 4
    WORKER 5 somethingW5.2

    This is my VBA code:
    Sub sampleSearch()
    
                Dim wsh as Long
                wsh = 5
                Dim lastRow As Long
                lastRow = Worksheets(wsh).Cells(Rows.Count, 1).End(xlUp).Row
     
                Dim name As String: name = "*"
                Dim rgSearch As Range
                Set rgSearch = Worksheets(wsh).Range("B1:D" & lastRow)
                Dim cell As Range
                Set cell = rgSearch.Find(name)
                
                    If cell Is Nothing Then
                        Debug.Print "Not found"
                        GoTo NothingFound
                    End If
                
                    ' Store first cell address
                    Dim firstCellAddress As String
                    firstCellAddress = cell.Address
                
                    ' Find all cells containing NAME
                    Do
    
                        If cell = "NAME" Or cell = "VALUE 1" Or cell = "VALUE 2" Or cell = "VALUE 3" Then GoTo SkipToNextFound
                        Debug.Print "Found: " & cell.Address(False, False)
                        Debug.Print Worksheets(wsh).Range("A" & cell.Row) & cell.Row
                        
    SkipToNextFound:
                        Set cell = rgSearch.FindNext(cell)
                    Loop While firstCellAddress <> cell.Address
        
    NothingFound:
        Debug.Print Worksheets(wsh).name & " " & lastRow & " " & rgSearch.Address(False, False) & vbCr & "END"
    End Sub
    My Immediate window displays result like this:
    Found: B2
    WORKER 1 2
    Found: C2
    WORKER 1 2
    Found: C3
    WORKER 2 3
    Found: D3
    WORKER 2 3
    Found: B4
    WORKER 3 4
    Found: D4
    WORKER 3 4
    Found: C6
    WORKER 5 6
    I need somethin like this:
    Found: B2
    WORKER 1 2
    Found: C3
    WORKER 2 3
    Found: B4
    WORKER 3 4
    Found: C6
    WORKER 5 6

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA find on hit go to next row

    Well - it's not pretty:

    Sub sampleSearch()
    
                Dim wsh As String
                wsh = "5"
                Dim lastRow As Long
                lastRow = Worksheets(wsh).Cells(Rows.Count, 1).End(xlUp).Row
     
                Dim name As String: name = "*"
                Dim rgSearch As Range
                Set rgSearch = Worksheets(wsh).Range("B1:D" & lastRow)
                Dim cell As Range
                Set cell = rgSearch.Find(name)
                
                    If cell Is Nothing Then
                        Debug.Print "Not found"
                        GoTo NothingFound
                    End If
                
                    ' Store first cell address
                    Dim firstCell As Range
                    Dim firstCellAddress As String
                    firstCellAddress = cell.Address
                    Set firstCell = Range(firstCellAddress)
                
                    ' Find all cells containing NAME
                   Do
                        If cell = "NAME" Or cell = "VALUE 1" Or cell = "VALUE 2" Or cell = "VALUE 3" Then GoTo SkipToNextFound
                                 Debug.Print "Found: " & cell.Address(False, False)
                                 Debug.Print Worksheets(wsh).Range("A" & cell.Row) & cell.Row                    
    SkipToNextFound:
                        Set cell = rgSearch.FindNext(After:=Cells(cell.Row, 4))
                   Loop While firstCell.Row <> cell.Row
        Exit Sub
    NothingFound:
        Debug.Print Worksheets(wsh).name & " " & lastRow & " " & rgSearch.Address(False, False) & vbCr & "END"
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    12-09-2009
    Location
    Belgrade
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA find on hit go to next row

    it works if I put it like this

    Set cell = rgSearch.FindNext(After:=Cells(cell.Row + 1, 2))

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA find on hit go to next row

    
    
    Sub sampleSearch()
    
                Dim wsh As Long
                Dim lastRow As Long
                Dim name As String: name = "*"
                Dim rgSearch As Range
                Dim cell As Range
                Dim Flag As Boolean
                            
                lastRow = Worksheets(wsh).Cells(Rows.Count, 1).End(xlUp).Row
                wsh = 5
                Flag = False
                
        For Count = 1 To lastRow
                Set rgSearch = Worksheets(wsh).Range("B" & Count & ":D" & Count)
                Set cell = rgSearch.Find(name)
                
                    If Not cell Is Nothing Then
    
                        If cell <> "NAME" And cell <> "VALUE 1" And cell <> "VALUE 2" And cell <> "VALUE 3" Then
                        Debug.Print "Found: " & cell.Address(False, False)
                        Debug.Print Worksheets(wsh).Range("A" & cell.Row) & cell.Row
                        Flag = True
                        End If
                        
                    End If
                    
        Next
                    
        If Flag = False Then
                        Debug.Print "Not found"
                        Debug.Print Worksheets(wsh).name & " " & lastRow & " " & rgSearch.Address(False, False) & vbCr & "END"
        End If
        
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA find on hit go to next row

    All that work and we could have just changed the original find line to:

    Set cell = rgSearch.Find(name, , , , xlByRows)
    Thanks for the rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find pattern in different sets of similar data and compare/find the closest ones.
    By Sarangsood in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-16-2020, 09:46 AM
  2. Replies: 3
    Last Post: 01-15-2019, 03:58 AM
  3. Replies: 1
    Last Post: 04-10-2017, 03:33 PM
  4. Replies: 3
    Last Post: 08-18-2015, 09:04 AM
  5. Replies: 9
    Last Post: 12-21-2014, 06:29 PM
  6. Replies: 4
    Last Post: 09-18-2012, 10:54 AM
  7. Replies: 1
    Last Post: 01-31-2006, 06:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1