+ Reply to Thread
Results 1 to 11 of 11

search & replace macro

Hybrid View

LiamF search & replace macro 07-30-2010, 06:18 AM
pike Re: search & replace macro 07-30-2010, 08:23 AM
pike Re: search & replace macro 08-03-2010, 07:01 AM
Shaner73 Re: search & replace macro 08-03-2010, 08:36 AM
pike Re: search & replace macro 08-04-2010, 12:01 AM
ajaykgarg Re: search & replace macro 08-04-2010, 03:48 AM
pike Re: search & replace macro 08-04-2010, 04:37 AM
ajaykgarg Re: search & replace macro 08-04-2010, 04:42 AM
pike Re: search & replace macro 08-04-2010, 05:09 AM
Shaner73 Re: search & replace macro 08-04-2010, 01:13 PM
Alf Re: search & replace macro 08-04-2010, 02:41 PM
  1. #1
    Registered User
    Join Date
    12-12-2006
    Posts
    63

    search & replace macro

    Hi, I'm using search & replace in a macro but only want to replace the text if it is in the first 3 letters within the cell. So, for example, I currently have;


    Selection.Replace What:="DS", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    This is to delete the text "DS" but obviously it's deleting DS anywhere within the text rather than just at the beginning of the text.

    any help much appreciated.

    Liam
    Last edited by LiamF; 07-30-2010 at 06:57 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: search & replace macro

    Hi LiamF

    maybe something like
    Option Explicit
    Sub Tester()
    Dim c
    Dim RegEx As RegExp
        Set RegEx = New RegExp
        With RegEx
            .MultiLine = False
            .Global = True
            .IgnoreCase = True
            .Pattern = "^DS"
      For Each c In Selection
            c.Value = RegEx.Replace(c.Value, "")
        Next
    End With
    Set RegEx = Nothing
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: search & replace macro

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  4. #4
    Registered User
    Join Date
    07-21-2010
    Location
    Largo, FL
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    54

    Re: search & replace macro

    Since you already have your code, add this about the code (but change the column to the column your data is in...

    Columns("A:A").Select
    Your finished code will look like this....

    Columns("A:A").Select
    
    Selection.Replace What:="DS", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: search & replace macro

    Hi Shaner73
    Doesnt the code will still replace DS any where in the text?

  6. #6
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: search & replace macro

    I think
    LookAt:=xlPart
    should be replaced with
    LookAt:=xlWhole
    hth
    Ajay

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: search & replace macro

    Hmmm. same problem with samples
    DSAAAA
    AADSAA
    AAAADS

  8. #8
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: search & replace macro

    Sorry, I missed the following in original post.

    replace the text if it is in the first 3 letters

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: search & replace macro

    Hi ajaykgarg
    No that doesnt work
    Sub Tester()
    Dim c
    Dim RegEx As Object
        Set RegEx = CreateObject("VBScript.RegExp")
        With RegEx
            .MultiLine = False
            .Global = True
            .IgnoreCase = True
            .Pattern = "^DS"
      For Each c In Selection
            c.Value = RegEx.Replace(c.Value, "")
        Next
    End With
    Set RegEx = Nothing
    End Sub

  10. #10
    Registered User
    Join Date
    07-21-2010
    Location
    Largo, FL
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    54

    Re: search & replace macro

    Pike, you are correct. I didn't read the original post well enough. I took his post as "DS" was being removed from all columns and rows. My apologies.

    In the mean time, I'm looking to see if I have anything else that may be able to help him.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: search & replace macro

    Perhaps something like this

    Sub tester()
    Dim c As Range
    For Each c In ActiveSheet.UsedRange
    If Left(c, 3) Like ("DS?") Or Left(c, 3) Like ("?DS") Then
              c.Replace What:="DS", Replacement:=""
        End If
        Next c
    End Sub
    HTH

    Alf
    Last edited by Alf; 08-04-2010 at 04:15 PM. Reason: Better answar

+ 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