+ Reply to Thread
Results 1 to 6 of 6

find and replace specific characters

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    15

    find and replace specific characters

    This replace only seems to work when it mataches an entire cell value, but i simply want to replace specific characters, like ', or ) or (, located withing cells, anywhere on the sheet, it doesn't replace.

        Set rng = ActiveSheet.UsedRange
        rng.Replace what:="(", replacement:=""
        rng.Replace what:=")", replacement:="-"

    Any ideas?

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: find and replace specific characters

       with Sheets(1).UsedRange
        .Replace "(", "",xlpart
        .Replace ")", "-" ,xlpart
       end with
    Last edited by snb; 08-21-2010 at 11:50 AM.

  3. #3
    Registered User
    Join Date
    08-12-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: find and replace specific characters

    Thank you... this is causing everything in those cells to be removed.

    If it makes a difference, there's a lot going on in this macro... here's the whole thing:

    Sub FORMATUSERS()
    
    ActiveSheet.Name = "user_info"
     
       With Sheets(1).UsedRange
        .Replace "*(*", ""
        .Replace "*)*", "-"
       End With
    
    For c = 42 To 61
        Range(Cells(2, c), Cells(2, c).End(xlDown)).Replace "T", Cells(1, c), xlWhole
        Range(Cells(2, c), Cells(2, c).End(xlDown)).Replace "F", "", xlWhole
    Next c
    
    'Find last column with data in Row 1
        lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    'Loop through columns, starting at the last one
        For delCol = lastCol To 1 Step -1
    'Delete columns with not_used in Row 1
        If Cells(1, delCol) = "not_used" Then _
        Cells(1, delCol).EntireColumn.Delete
    Next
    
    Dim Cell As Range
    Application.ScreenUpdating = False
    
        For Each Cell In Range("1:1").SpecialCells(xlConstants)
            Cell = Replace(Cell, " ", "_")
            Cell = Replace(Cell, "|", "_")
        Next Cell
        
    End Sub

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: find and replace specific characters

    maybe
    Sub Macro1()
    
    
    Set rng = ActiveSheet.UsedRange
    With rng
        rng.Replace What:="(", Replacement:="", LookAt:=xlPart
        rng.Replace What:=")", Replacement:="", LookAt:=xlPart
     End With
    End Sub
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: find and replace specific characters

    Sub FORMATUSERS()
       ActiveSheet.Name = "user_info"
     
       With Sheets("user_info").UsedRange
        .Replace "(", "",xlpart
        .Replace ")", "-",xlpart
       End With
    
        with cells(2,42).end(xldown).resize(,19)
          .replace "T",cells(1,42), xlwhole
          .Replace "F", "", xlWhole
       end with
    
      with columns(1)
         .replace "not used", ""
         .specialcells(4).entirerow.delete
         .specialcells(2).replace  " ", "_"
         .specialcells(2).replace "|", "_"
      end with
    End Sub

  6. #6
    Registered User
    Join Date
    08-12-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: find and replace specific characters

    This worked perfectly.

    Thank you forum!

    Sub Macro1()
    
    
    Set rng = ActiveSheet.UsedRange
    With rng
        rng.Replace What:="(", Replacement:="", LookAt:=xlPart
        rng.Replace What:=")", Replacement:="", LookAt:=xlPart
     End With
    End Sub

+ 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