|
-
Mar 22nd, 2013, 06:08 AM
#1
Thread Starter
PowerPoster
[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.
-
Mar 22nd, 2013, 07:14 AM
#2
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
-
Mar 22nd, 2013, 07:26 AM
#3
Thread Starter
PowerPoster
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.
-
Mar 22nd, 2013, 07:28 AM
#4
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.
-
Mar 25th, 2013, 02:53 AM
#5
Thread Starter
PowerPoster
Re: Remove specific from 2 columns (VBA)
Do you have an example on how to do that?
Thanks in advance.
-
Mar 25th, 2013, 07:05 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|