Results 1 to 5 of 5

Thread: [RESOLVED] Find all occurrence of a string in E:E from multiple worksheets.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Resolved [RESOLVED] Find all occurrence of a string in E:E from multiple worksheets.

    I have Worsheets(1) is where the searching occurs. Column E of Worksheets(2 to n) will be searched.
    In worksheet(1) I have a textbox the receives the string to be searched.

    Code:
    countWS = ThisWorkbook.Sheets.Count
    resultRow = 5
    For cursor = 2 To countWS
       'seach and paste each result on Worksheets(1).Cells(resultRow, 2)
       'resultRow = resultRow +1
    Next cursor

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find all occurrence of a string in E:E from multiple worksheets.

    Code:
     for ws = 2 to thisworkbook.sheets.count
        set fnd = thisworkbook.sheets(ws).cells(rows.count, 5).end(xlup)
        do    
           set fnd = thisworkbook.sheets(ws).range("e:e").find(sheet1.textbox1.value, fnd)
           if fnd is nothing then exit do
           thisworkbook.sheets(1).cells(resultrow, 2).value = fnd & " at " & thiswokbook.sheets(ws).name & " " & fnd.address
        loop
    next
    this code must be written in a procedure in sheets(1) module, assumes sheets(1) is sheet1 (whichever sheet has the textbox)

    i did not test this, so there could be some error
    Last edited by westconn1; Dec 4th, 2013 at 06:48 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Find all occurrence of a string in E:E from multiple worksheets.

    Quote Originally Posted by westconn1 View Post
    Code:
     for ws = 2 to thisworkbook.sheets.count
        set fnd = thisworkbook.sheets(ws).cells(rows.count, 5).end(xlup)
        do    
           set fnd = thisworkbook.sheets(ws).range("e:e").find(sheet1.textbox1.value, fnd)
           if fnd is nothing then exit do
           thisworkbook.sheets(1).cells(resultrow, 2).value = fnd & " at " & thiswokbook.sheets(ws).name & " " & fnd.address
        loop
    next
    this code must be written in a procedure in sheets(1) module, assumes sheets(1) is sheet1 (whichever sheet has the textbox)

    i did not test this, so there could be some error
    Hi, I test-searched a string that supposed to just return 4 results. When I ran the code it looped forever. Instead of displaying the results I change it to count the results just so I can show you. I used Ctrl-Break to stop. Also it doesn't move to the next sheet. It just loops the second sheet.
    Code:
    Private Sub SearchBox_GotFocus()
        SearchBox.BackColor = vbWhite
    End Sub
    
    Private Sub SearchButton_Click()
        If SearchBox.Text = "" Then
            SearchBox.BackColor = vbRed
            MsgBox "Search parameter is empty. Please fill-in the highlighted box.", vbCritical
        Else
            Find_String
        End If
    End Sub
    
    Private Sub Find_String()
        Dim searchString As String
        Dim countWS, counter As Integer
        Dim ws As Integer
        Dim resultRow As Integer
        
        searchString = SearchBox.Value
        countWS = ThisWorkbook.Sheets.Count
        resultRow = 5
        counter = 0
    
        For ws = 2 To countWS
            Set fnd = ThisWorkbook.Sheets(ws).Cells(Rows.Count, 5).End(xlUp)
            Do
                Set fnd = ThisWorkbook.Sheets(ws).Range("E:E").Find(searchString, fnd)
                If fnd Is Nothing Then
                    ThisWorkbook.Sheets(1).Cells(resultRow, 2).Value = "NO RESULTS FOUND"
                    Exit Do
                Else
                    'ThisWorkbook.Sheets(1).Cells(resultRow, 2).Value = fnd
                    'ThisWorkbook.Sheets(1).Cells(resultRow, 3).Value = Sheets(ws).Name
                    'ThisWorkbook.Sheets(1).Cells(resultRow, 4).Value = fnd.Address
                    'resultRow = resultRow + 1
                    ThisWorkbook.Sheets(1).Cells(1, 2).Value = counter
                    counter = counter + 1
                End If
            Loop
        Next ws
    End Sub

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find all occurrence of a string in E:E from multiple worksheets.

    i have tested this code, it will exit from the do loop correctly replace the original content of my for loop with
    Code:
    Set firstfnd = s.Range("a1")
    Set fnd = s.Cells(Rows.Count, 5).End(xlUp).Offset(1)
    Do
    Set fnd = s.Range("e:e").Find("gold", fnd)
    If fnd Is Nothing Then Exit Do
    If fnd.Address = firstfnd.Address Then Exit Do
    
    Debug.Print fnd.parent.name, fnd.Address
    If firstfnd.Address = "$A$1" Then Set firstfnd = fnd
    cnt = cnt + 1
    Loop
    Set firstfnd = s.Range("a1")
    replace sheet object s with thisworkbook.sheets(ws) in all instances, i did not test the for loop, but doubt that it will give any serious problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Find all occurrence of a string in E:E from multiple worksheets.

    thank you!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width