+ Reply to Thread
Results 1 to 7 of 7

Replace "~" in Cell of Col. A through the cell before

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-12-2007
    Location
    Germany
    MS-Off Ver
    2007
    Posts
    472

    Replace "~" in Cell of Col. A through the cell before

    Hello everyone,

    i have to replace "~" in column A through the content of cell before which has no "~". I have to process many thousands of dictionary words. For example:


    Col. A (before)
    long
    love
    ~ly
    ~one
    making ~
    be~d
    lou
    loud
    ~ly


    After running th macro it should give following results:

    Col. A (after)
    long
    love
    lovely
    loveone
    making love
    beloved
    lou
    loud
    loudly

    Thank you very much for each help in advance.

    Please also refer to example excel file!!
    Attached Files Attached Files
    Last edited by wali; 10-11-2009 at 01:44 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Replace "~" in Cell of Col. A through the cell before

    One approach using Evaluate

    Public Sub Example()
    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp))
        .Value = Evaluate("IF(ROW(" & .Address & "),SUBSTITUTE(" & .Address & ",""~""," & .Offset(-1).Address & "))")
    End With
    End Sub
    EDIT: ignore the above, I failed to notice consecutive ~ requirement

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Replace "~" in Cell of Col. A through the cell before

    Just on an aside - do you definitively need VBA, ie based on your sample

    E2:
    =IF(ISERR(SEARCH("~~",$A2)),$A2,SUBSTITUTE($A2,"~",LOOKUP(2,1/(ISERR(SEARCH("~~",$A$1:$A1))),$A$1:$A1)))

    copied down should give you your results, no ?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Replace "~" in Cell of Col. A through the cell before

    Hello Wali,

    This macro has been added to the attached workbook. I have run it, and from what I seems to work correctly. But, you need to be the judge.
    Sub CompoundWords()
    
      Dim Data() As Variant
      Dim I As Long
      Dim R As Long
      Dim RegExp As Object
      Dim Rng As Range
      Dim RngEnd As Range
      Dim RootWord As String
      Dim S As String
      Dim Wks As Worksheet
      
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Global = True
        RegExp.Pattern = "(~)"
        
          Set Wks = ActiveSheet
          
          Set Rng = Wks.Range("A1")
          Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
          
          ReDim Data(1 To Rng.Rows.Count, 1 To 1)
          Data = Rng.Value
          
            RootWord = Data(1, 1)
            For I = 2 To UBound(Data, 1)
              S = Data(I, 1)
              If RegExp.Test(S) = True Then
                 Data(I, 1) = RegExp.Replace(S, RootWord)
              Else
                 RootWord = S
              End If
            Next I
          
          Rng.Value = Data
          
        Set RegExp = Nothing
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    11-12-2007
    Location
    Germany
    MS-Off Ver
    2007
    Posts
    472

    Re: Replace "~" in Cell of Col. A through the cell before

    Thank you very much to both of you! Million thanks

    Dear DonkeyOte

    your code works but only for the first "~" but not for the following one. And I cant run the formula cus it shows error. May be because i am using German version of Excel. But thank you very much for your help.


    Dear LeitRoss

    Your code works perfectly and does what i wanted. You saved me alot of time and nerves. Thank you very very much. God bless you

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Replace "~" in Cell of Col. A through the cell before

    Wali,

    If your list starts in A1, DO's formula goes in B2 and is copied down from there. It works perfectly.

    May be because i am using German version of Excel.
    DO has a link in his sig to function name translations, which should be right up your alley.
    Last edited by shg; 10-11-2009 at 05:46 PM.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor
    Join Date
    11-12-2007
    Location
    Germany
    MS-Off Ver
    2007
    Posts
    472

    Re: Replace "~" in Cell of Col. A through the cell before

    Dear Shg and Do

    thank you very much. Now after translating the function it works perfectly. Thank you very much

+ 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