Consulting

Results 1 to 7 of 7

Thread: Replace Exact String - Array

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Replace Exact String - Array

    Good Morning folks,

    any one know how i can search and replace for exact only,

    my array has

    FCPH and CPH so i get that replaced twice

    as FCPH contains CPH



     Sub Replace_Exact()
        
        
        oSearch = Array("Apple", "Pear", "FCPH", "CPH")
    
        For i = LBound(oSearch) To UBound(oSearch)
        
        For Each ocell In ThisWorkbook.Worksheets("AA").Range("A1:A100").Cells
        
        ocell.Replace What:=oSearch(i), Replacement:="test"
        
      
        Next ocell
        Next i
     
      End Sub
    how can i limit the search to exact only ?
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Is F1 in your VBEditor locked/absent ?

    The second loop is redundant.

  3. #3
    Hello
    Try this line instead
    oCell.Replace What:=oSearch(i), Replacement:="test", LookAt:=xlWhole

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub Replace_Exact()
         Dim oSearch
         Dim i As Long
         
        oSearch = Array("Apple", "Pear", "FCPH", "CPH")
    
        For i = LBound(oSearch) To UBound(oSearch)     
            ThisWorkbook.Worksheets("AA").Range("A1:A100").Replace _
                            What:=oSearch(i), Replacement:="test", _
                            LookAt:=xlWhole, MatchCase:=True, matchbyte:=True
        Next i
         
    End Sub

    マナ

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @mana

    Why not ?

    Sub Replace_Exact()
      For each it in  Array("Apple", "Pear", "FCPH", "CPH")
        ThisWorkbook.sheets("AA").Range("A1:A100").Replace it, "test", 1
      Next
    End Sub

  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you Yasser and Mana,

    LookAt:=xlWhole, MatchCase:=True, matchbyte:=True,

    Yes this is the line that I was looking for

    Have a great day !
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you snb,
    let me experiment with this version as well
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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