+ Reply to Thread
Results 1 to 5 of 5

Thread: Replace each "§" to "µ" but not those followed by space or "."

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

    Replace each "§" to "µ" but not those followed by space or "."

    Hello everyone,
    I need a macro which does following for the selected area:

    1a. Step
    Each "§" should be replaced by "µ" in whole document but not those which are followed by "." or ";" or "»" or "«" or "(" or ")" or followed by space.
    1b. Step
    Change the color of new added "µ" to red.

    2. Step
    Each "~" should be replaced by "&" but not those which have "." or ";" or "»" or "«" or "(" or ")" or space befor it.
    2b. Step
    Change the color of new added "&" to red.

    3. Step
    Show message how many "§" and how many "~" were added.



    Example:
    Germa§ vot§r§. ~a~e re~ §av~.


    should become:

    Germa§ votµr§. ~a&e re& §av&.

    Thank you very much for each assistance in advance.



    Posted in Word programming area as well:
    Attached Files Attached Files
    Last edited by wali; 09-30-2009 at 11:04 AM.

  2. #2
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Replace each "§" to "µ" but not those followed by space or "."

    I've made a Custom Function. I can't get the red right yet, but here is the intermediate solution:

    Function ReplaceStr(SourceStr As Range) As String
    
    '"§" replaced by "µ"  not those which are followed by "." or ";" or "»" or "«" or "(" or ")" or followed by space.
    '"~" replaced by "&" "." or ";" or "»" or "«" or "(" or ")" or space before it.
      Dim TmpChar As String, TmpBefore As String, TmpAfter As String
      
      ReplaceStr = ""
      For CharCounter = 1 To Len(CStr(SourceStr)) 'Loop through characters in string
        TmpChar = Mid(CStr(SourceStr), CharCounter, 1)
        
        If CharCounter > 1 And CharCounter < Len(SourceStr) Then
          TmpBefore = Mid(SourceStr, CharCounter - 1, 1)
          TmpAfter = Mid(SourceStr, CharCounter + 1, 1)
        ElseIf CharCounter = 1 Then
          TmpBefore = ""
          TmpAfter = Mid(SourceStr, CharCounter + 1, 1)
        ElseIf CharCounter = Len(SourceStr) Then
          TmpBefore = Mid(SourceStr, CharCounter - 1, 1)
          TmpAfter = ""
        End If
        
        Select Case TmpChar
          Case "§"
            Select Case TmpAfter
              Case ".", ";", "»", "«", "(", ")", " "
                'Do Nothing
              Case Else
                TmpChar = "µ"
            End Select
          Case "~"
            Select Case TmpBefore
              Case ".", ";", "»", "«", "(", ")", " "
                'Do Nothing
              Case Else
                TmpChar = "&"
            End Select
          Case Else
            'Do Nothing
        End Select
        
        ReplaceStr = ReplaceStr & TmpChar
      
      Next CharCounter
      
    End Function
    If the String is in A1 then place this formula
    =ReplaceStr(A1)
    in another cell
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Valued Forum Contributor
    Join Date
    11-12-2007
    Location
    Germany
    MS-Off Ver
    2007
    Posts
    389

    Re: Replace each "§" to "µ" but not those followed by space or "."

    Thank you very much. It works very good. I need this for MS word. Is it possible to have the same function as makro for MS word or for Excel?

    Thank you very much

  4. #4
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Replace each "§" to "µ" but not those followed by space or "."

    This is an excel forum, so maybe you have to post the same request at a word forum.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

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

    Re: Replace each "§" to "µ" but not those followed by space or "."

    Ok! Thank you very much. It works perfect in excel.

+ 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.2.0