Results 1 to 6 of 6

Thread: [RESOLVED] Remove specific from 2 columns (VBA)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Remove specific from 2 columns (VBA)

    Hi,

    I have to remove/replace a couple of specific characters ( "-", "(",")","/" ) from Columns "O" & "X" (UsedRange). Anyone knows how to code this? I know I can use "Replace What:=", but how to with multiple characters? Thanks in advance.
    Last edited by Radjesh Klauke; Mar 22nd, 2013 at 06:18 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Remove specific from 2 columns (VBA)

    There's probably a better way than this, especially if you have more than a few characters to replace. This example replaces 3 characters (stored in variables), found in column A, with replacement text.

    Code:
    Sub replaceThem()
        Dim rep1 As String
        Dim rep2 As String
        Dim rep3 As String
        Dim repWith As String
        
        rep1 = ","
        rep2 = "("
        rep3 = ")"
        repWith = "abc"
        
        Range("a1").EntireColumn.Replace what:=rep1, replacement:=repWith, lookat:=xlPart, _
            searchorder:=xlByRows, MatchCase:=False, searchformat:=False, ReplaceFormat:=False
        Range("a1").EntireColumn.Replace what:=rep2, replacement:=repWith, lookat:=xlPart, _
            searchorder:=xlByRows, MatchCase:=False, searchformat:=False, ReplaceFormat:=False
        Range("a1").EntireColumn.Replace what:=rep3, replacement:=repWith, lookat:=xlPart, _
            searchorder:=xlByRows, MatchCase:=False, searchformat:=False, ReplaceFormat:=False
    End Sub

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remove specific from 2 columns (VBA)

    It's better then nothing m8. If someone has a better solution, i'd love to hear it.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Remove specific from 2 columns (VBA)

    One thing you could do is create an array of characters to be replaced, then loop through the "replace" code, rather than repeating it.

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remove specific from 2 columns (VBA)

    Do you have an example on how to do that?
    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Remove specific from 2 columns (VBA)

    Code:
    Sub replaceThem()
        Dim rep1 As String
        Dim rep2 As String
        Dim rep3 As String
        Dim repWith As String
        Dim replChars(3) As String
        Dim i As Integer
        Dim rng As Range
        
        replChars(0) = ","
        replChars(1) = "("
        replChars(2) = ")"
        replChars(3) = "-"
        repWith = "abc"
        Set rng = Range("a1").EntireColumn
        
        For i = 0 To 3
            rng.Replace what:=replChars(i), replacement:=repWith, lookat:=xlPart, _
            searchorder:=xlByRows, MatchCase:=False, searchformat:=False, ReplaceFormat:=False
        Next i
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width