View Single Post
  #3   Report Post  
Terry Pinnell
 
Posts: n/a
Default Macro to remove text

Dave Peterson wrote:

You can get very close to what you want by selecting column D
edit|replace
what: (*) 'open paren, asterisk, close paren
with: (leaveblank) 'don't type anything--nothing!
replace all

The use a helper column of formulas like:

=trim(d1)
(and drag down)

Then convert to values and delete the original column D.

In code, it would look something like:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCell As Range

Set wks = Worksheets("sheet1")

With wks
Set myRng = .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
End With

myRng.Replace what:="(*)", replacement:="", _
lookat:=xlPart, searchorder:=xlByRows, _
MatchCase:=False

For Each myCell In myRng.Cells
myCell.Value = Application.Trim(myCell.Value)
Next myCell

End Sub


Thanks very much, kind of you to take the trouble. I'll give it a try.
Meanwhile I resorted to RegEx! I eventually managed to do it by
pasting that column into my text editor and using a Find of
(.+)
and replacing it with nothing.

(Note that TextPad's RegEx implementation is unusual. The equivalent
in 'standard PERL' would be
\(.+\)

Could you remind me in really no-brainer terms what steps I take to
get that code pasted correctly into Excel 2000 please? As you may have
seen from my earlier post, I've forgotten what little I once learned
<g.

BTW, that earlier example from Don did not have an 'Option Explicit'
line at the top. I see mine 'inherited' one somehow. I vaguely recall
I set it up to do that automatically years ago, and suspect this may
be implicated in my difficulties?

--
Terry, West Sussex, UK