+ Reply to Thread
Results 1 to 16 of 16

macro replace a string with certain text..?

Hybrid View

adbasanta macro replace a string with... 03-09-2012, 06:53 AM
Domski Re: macro replace a string... 03-09-2012, 06:55 AM
Kyle123 Re: macro replace a string... 03-09-2012, 06:55 AM
Firefly2012 Re: macro replace a string... 03-09-2012, 07:03 AM
adbasanta Re: macro replace a string... 03-09-2012, 07:13 AM
adbasanta Re: macro replace a string... 03-09-2012, 07:19 AM
Firefly2012 Re: macro replace a string... 03-09-2012, 07:23 AM
adbasanta Re: macro replace a string... 03-09-2012, 08:35 AM
Firefly2012 Re: macro replace a string... 03-09-2012, 08:52 AM
adbasanta Re: macro replace a string... 03-09-2012, 09:11 AM
Domski Re: macro replace a string... 03-09-2012, 07:28 AM
adbasanta Re: macro replace a string... 03-09-2012, 08:37 AM
Domski Re: macro replace a string... 03-09-2012, 08:46 AM
adbasanta Re: macro replace a string... 03-09-2012, 09:07 AM
Kyle123 Re: macro replace a string... 03-09-2012, 09:24 AM
adbasanta Re: macro replace a string... 03-09-2012, 09:40 AM
  1. #1
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Question macro replace a string with certain text..?

    Good day to all!

    Im just having a little problem on how to replace a certain string characters with new format via macro.

    Example:

    cbu-juan, dela cruz

    i would like to make it to

    cbu - juan, dela cruz

    the changes is that, there is a space before and after the dash.

    thank you..

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: macro replace a string with certain text..?

    What code do you have so far? The macro recorder would give you the syntax you need using a Find...Replace.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: macro replace a string with certain text..?

    How about:
    newString = Replace(OldString,"-"," - ")

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: macro replace a string with certain text..?

    If you already have values within the sheet which have the correct " - " structure, then you may want to TRIM the result eg:

    newString = Application.Trim(Replace(OldString,"-"," - "))
    This ensures you don't end up with multiple spaces either side of the -

  5. #5
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    I have this code as you guys suggested, but it seems that the whole content of the column has been erased.

    Sub replace_str_format()
    Dim counter As Long
        For counter = 1 To 131
            With Sheet1
                .Cells(counter, 1) = Trim(Replace(OldString, "-", " - "))
            End With
        Next
    End Sub
    it should only find the "-" in a word and replace it with " - "

    thank you
    Last edited by adbasanta; 03-09-2012 at 07:16 AM.

  6. #6
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    sorry guys..I can't believe I missed to replace the oldstring variable with mine..
    here is the modified version.

    Sub replace_str_format()
    Dim counter As Long
        For counter = 1 To 131
            With Sheet1
                .Cells(counter, 1) = Trim(Replace(.Cells(counter, 1), "-", " - "))
            End With
        Next
    End Sub
    Thank you Domski, Kyle123, Firefly2012 for such a straight to the point solution.

  7. #7
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: macro replace a string with certain text..?

    Remark: you need to use Application.Trim as this is the worksheetfunction version that converts "dog Cat" to "dog Cat" ie internal multiple spaces are trimmed. You are currently using the VBA version which will only trim leading/trailing spaces (ie internal ones are ignored).

  8. #8
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    Quote Originally Posted by Firefly2012 View Post
    Remark: you need to use Application.Trim as this is the worksheetfunction version that converts "dog Cat" to "dog Cat" ie internal multiple spaces are trimmed. You are currently using the VBA version which will only trim leading/trailing spaces (ie internal ones are ignored).
    thank you Firefly2012, but how to convert this code to application.trim? I cannot use the trim with application since it will not show as a function of the application when i press dot..
    here is code so far.

    Sub replace_str_format()
    Dim counter As Long
        For counter = 1 To 131
            With Sheet1
                .Cells(counter, 1) = Trim(Replace(.Cells(counter, 1), "-", " - "))
            End With
        Next
    End Sub

  9. #9
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: macro replace a string with certain text..?

    Quote Originally Posted by adbasanta View Post
    thank you Firefly2012, but how to convert this code to application.trim? I cannot use the trim with application since it will not show as a function of the application when i press dot..
    here is code so far.
    Just because it doesn't show up in Intellisense doesn't mean you can't use it (if this was the case, you'd be screwed every time you tried to use Activesheet)

    Did you try it with Application.Trim?

  10. #10
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    Quote Originally Posted by Firefly2012 View Post
    Just because it doesn't show up in Intellisense doesn't mean you can't use it (if this was the case, you'd be screwed every time you tried to use Activesheet)

    Did you try it with Application.Trim?
    Thank you Firefly2012. It works perfectly. I can't believe such Intellisense doesn't show-up. Im sorry but im not a hardcoder in Excel. My ground is VB, but I love to do it in excel to.Thank you..
    Sub replace_str_format()
    Dim counter As Long
        For counter = 1 To 260
            With Sheet4
                .Cells(counter, 3) = Application.Trim(Replace(.Cells(counter, 3), "-", " - "))
            End With
        Next
    End Sub

  11. #11
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: macro replace a string with certain text..?

    If you don't already have " - " and can avoid the trim then this would suffice:

    Range("A1:A131").Replace What:="-", Replacement:=" - ", LookAt:=xlPart
    Dom

  12. #12
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    thank you Domski, will this work with internal "-"?.

  13. #13
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: macro replace a string with certain text..?

    Should do.

    Dom

  14. #14
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    thank you Dom.
    Quote Originally Posted by Domski View Post
    Should do.

    Dom

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: macro replace a string with certain text..?

    This is probably a little faster - does away with the loop:
    Dim rng As Range
    Set rng = Sheet4.Range("C1:C260")
    rng.Value = Split(Application.Trim(Replace(Join(Application.Transpose(rng), "|\"), "-", " - ")), "|\")

  16. #16
    Registered User
    Join Date
    10-05-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: macro replace a string with certain text..?

    Quote Originally Posted by Kyle123 View Post
    This is probably a little faster - does away with the loop:
    Dim rng As Range
    Set rng = Sheet4.Range("C1:C260")
    rng.Value = Split(Application.Trim(Replace(Join(Application.Transpose(rng), "|\"), "-", " - ")), "|\")
    thank you Kyle123. It seems the loading of this solution is faster, But when I execute the code, all values in column C has erased?
    Sub format_str()
    Dim rng As Range
    Set rng = Sheet2.Range("C1:C260")
    rng.Value = Split(Application.Trim(Replace(Join(Application.Transpose(rng), "|\"), "-", " - ")), "|\")
    End Sub
    Do I need to add some code in .Transpose area? It seems to be a complicated solution but its fast.

+ 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