beginner VBA question, creating macro to clean range

ivanwakeup

New Member
Joined
Aug 22, 2014
Messages
12
Hi guys,

very new to VBA, and i'm really interested in learning it because I find it fascinating. I know there are plenty of ways to do what i'm trying to do, but i just want to understand what it looks like in VBA so I can start picking up the language.

I want to be able to:

clean all characters I define from a given range of cells. if i could do it based on the name of the range or even the length/values within the cells in that range, that would be great. heres what I have so far, to give you an idea of what I'm trying to do:

Sub removechars()


For Each cell In Selection


ActiveCell.Value = Substitute(ActiveCell.Value, "+", "")
ActiveCell.Value = Substitute(ActiveCell.Value, "(", "")
ActiveCell.Value = Substitute(ActiveCell.Value, ")", "")
ActiveCell.Value = Substitute(ActiveCell.Value, "-", "")
Next cell


End Sub


I have no clue if this is even remotely on track or if my syntax is correct. When I try to run this, I get a compiler error: sub or function not defined. Again, what i'd like this macro to do eventually is find the proper named column within my worksheet ("Phone", in this case) and clean that range accordingly. Can someone offer any help?

Thanks very much in advance :)
 
But without looping that will still only find the first column, my understanding from post #7 was that there could be more than 1 'phone' column in some sheets.

Thanks Jason, I missed that from the way it was written.

Don't think that can be done with ranges, I did provide code for that in post #2, but again, it is in a loop.

I moved the Range loop into an array loop, and only write to the worksheet once, to run the Regexp for a 10x speed improvement.

The following doesn't look quite as concise but is fast (I ran 2 columns of 10,000 rows of dummy data in 0.2 sec.);

Code:
Sub CleanPhoneNumbers()

Dim Rng As Range
Dim rTestHeadingRange As Range
Dim sFirstAddress As String
Dim a
Const sFindHeading As String = "Phone"

a = Timer()
Application.ScreenUpdating = False
On Error Resume Next
With ActiveSheet
    Set rTestHeadingRange = .Rows("1:4")
    'Find first "Phone"
    Set Rng = rTestHeadingRange.Find(what:=sFindHeading, LookIn:=xlValues, lookat:=xlPart)
    'Check if match exists, if not exit sub
    If Rng Is Nothing Then
        MsgBox "Heading Not Found"
        Application.ScreenUpdating = True
        Exit Sub
    End If
    'Run removechars2 and check for other matches
    sFirstAddress = Rng.Address
    Do
        removechars2 Range(Rng.Offset(1), Application.Intersect(Rng.EntireColumn, .UsedRange.Rows(.UsedRange.Rows.Count)))
        Set Rng = rTestHeadingRange.FindNext(Rng)
    Loop While Not Rng Is Nothing And Rng.Address <> sFirstAddress

End With
Application.ScreenUpdating = True
'MsgBox Timer() - a & " seconds" 'uncomment this if you want to get a time check for processing.
End Sub

Sub removechars2(Rng As Range)
Dim Temp
Dim i As Long
Temp = Application.Transpose(Rng)
With CreateObject("vbscript.regexp")
    For i = 1 To UBound(Temp)
        .Pattern = "[^0-9]"
        .Global = True
        Temp(i) = .Replace(Temp(i), "")
        Temp(i) = Right(Temp(i), 9)
    Next
End With
Rng.Value = Application.Transpose(Temp)
End Sub

Teeroy
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I would never have thought of transposing the range to an array to process the data faster, that is a significant improvement over my methods.
 
Upvote 0
I would never have thought of transposing the range to an array to process the data faster, that is a significant improvement over my methods.

I think I orginially picked up the idea here from Rick Rothstein but Chip Pearson's web site has some really good information about how to use it.
 
Upvote 0
Teeroy, can you suggest good sources to learn about vbscript and regexp? I have never seen either, but they are obviously very powerful tools.
 
Upvote 0
dcardno,

I use Regular Expression Tutorial to get regex pattern info as I need it (I don't use it often enough to remember it :)).

For VBScript I have a couple of books but I don't actually use it much. Have a look at Tutorialpoint - VBScript; I just googled and this came up and it looked OK (for a quick glance). VBScript is a similar language to VB / VBA but the subtle differences can be frustrating. The only 2 things I think I use in VBA from VBScript are Regex and the Scripting Dictionary object.
 
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,356
Members
449,444
Latest member
abitrandom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top