Consulting

Results 1 to 2 of 2

Thread: Help with .Find and .FindNext in VBA

  1. #1

    Wink Help with .Find and .FindNext in VBA

    Hello,

    I have an excel spreadsheet with duplicate/similar names in it (in the thousands). I need to be able to find the duplicates/similar names by taking a substring from the original string value starting in cell (1,3) and comparing it to all other string values in that column of the worksheet. This means that I am not searching just for one string. I am searching first to see if there are duplicates for the string located in cell (1,3), then searching for duplicates to cell (2,3) and so on through the entire column in the worksheet. Right now my code only words for finding all the dupes for a specific string defined by me. How do I iterate through one entire column in the spreadsheet (changing the value I am searching on each time?)

    Code I have already.

    Sub FindAndExecute()
    
    
    Dim i As Integer
    Dim LastRow As Long
    Dim OriginalRange, SearchRange As Range
    Dim Sh As Worksheet
    Dim Loc As Range
    Dim Search, SubSearch As String
    LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
    OriginalRange = ActiveSheet.UsedRange
    SubSearch = Cells(11, 3).Value
    
    
    For Each Sh In ThisWorkbook.Worksheets
    With Sh.UsedRange
    Set Loc = .Cells.Find(What:=Mid(SubSearch, 1, 8), SearchDirection:=xlNext)
    If Not Loc Is Nothing Then
    Do Until Loc Is Nothing
    Loc.Value = "Answered!"
    Set Loc = .FindNext(Loc)
    Loop
    End If
    End With
    Set Loc = Nothing
    Next
    
    
    
    
    End Sub
        Next j
      Next i
    
    
    End Sub
    Last edited by Bob Phillips; 11-19-2015 at 09:53 AM. Reason: Added code tags

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the board.

    post your workbook:
    Go Advanced, Manage Attachments, Add Files, Select Files, (select the file/s and click Open), Upload Files, Done
    before posting it alter sensitive/confidential data.

    this way i may understand what you mean by "I am not searching just for one string"

    (i understand) you want to replace all substrings in all worksheets (other than active sheet i guess) with "Answered"?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

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
  •