Results 1 to 5 of 5

Need to replace X in text with A through F

Threaded View

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

    Re: Need to replace X in text with A through F

    Hello Adammj58,

    Here is another macro that uses Regular Expressions to find an replace the "X" with the letters you want. A button has been added to the attached workbook to run the macro.
    Sub ReplaceX()
    
      Dim Data As Variant
      Dim N As Integer
      Dim RegExp As Object
      Dim Rng As Range
      Dim RngEnd As Range
      Dim X As Long
      
        Set Rng = Range("B2")
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Range(Rng, RngEnd))
        
        Data = Array("A", "B", "C", "D", "E", "F")
        
        Set RegExp = CreateObject("VBscript.RegExp")
        RegExp.Pattern = "[xX]"
        
          For Each Cell In Rng
            If RegExp.Test(Cell.Text) Then
               N = X Mod 6
               Cell.Value = RegExp.Replace(Cell.Text, Data(N))
               X = X + 1
            End If
          Next Cell
         
        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!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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