+ Reply to Thread
Results 1 to 3 of 3

InStr command finding too many matches

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Unhappy InStr command finding too many matches

    Hi all,
    I have this which I am trying to input a list of names of which the match could be in any of 4 columns....

    This all works, BUT, if for example I put in 'sarah' and 'ddddsaarhcccc' is in the data dump then both 'sarah' and 'ddddsaarhcccc' are found.

    But i only want to find 'sarah'


    Can someone please help. I cant seem to work it out.


    
    'Clear all
    Ws4.Cells.ClearContents
    
    'First the Headers
    ws2.Range("2:2").Copy Destination:=PasteRng
    Set PasteRng = PasteRng.Offset(1, 0)
        
    Dim a As Long
    Dim search As Long
    Dim row As Long
    Dim col As Long
    Dim flag As Integer
        search = 1
        row = 3
        col = 4
        
    While Not Sheets("Servers To Find").Cells(search, 1) = Empty
        
           row = 3
           While Not Sheets("Physical Servers").Cells(row, 1) = Empty
           
           If InStr(LCase(Sheets("Physical Servers").Cells(row, col)), LCase(Sheets("Servers To Find").Cells(search, 1))) > 0 Then
           'If InStr(Sheets("Physical Servers").Cells(row, col), Sheets("Servers To Find").Cells(search, 1)) > 0 Then
           Sheets("Physical Servers").Cells(row, col).EntireRow.Copy Destination:=PasteRng
           Set PasteRng = PasteRng.Offset(1, 0)
           flag = 1
           End If
           row = row + 1
           
           Wend
        
        If flag = 0 Then
        col = 5
          row = 3
          While Not Sheets("Physical Servers").Cells(row, col) = Empty
        
           If InStr(LCase(Sheets("Physical Servers").Cells(row, col)), LCase(Sheets("Servers To Find").Cells(search, 1))) > 0 Then
           Sheets("Physical Servers").Cells(row, col).EntireRow.Copy Destination:=PasteRng
           Set PasteRng = PasteRng.Offset(1, 0)
           End If
           row = row + 1
           
           Wend
        End If
        
     col = 4
     search = search + 1
    
    Wend

  2. #2
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Re: InStr command finding too many matches

    I have included a dummy work book... i have put some dummy data into the physical and virtual worksheets.. and also included some search items in the 'servers to find' worksheet.

    Upon clicking the button the 'servers to find' WS you will see that it finds 'sarah' and '9999sarahcccc'...
    Attached Files Attached Files

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: InStr command finding too many matches

    Do not use instr.

    Sub snb()
      sheets(2).rows(5)=sheets(1).cells.find("Sarah",,xlvalues,xlWhole).entirerow.value
    End Sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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