View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners,microsoft.public.word.vba.general
Jezebel[_2_] Jezebel[_2_] is offline
external usenet poster
 
Posts: 6
Default Regular expressions for replacements in Excel?

If you are dealing with just one column of the spreadsheet, it might be
easier to copy it into a Word document, make the changes there, and copy it
back. Word's Find and Replace has the regular expression functions built in
(check the 'Use Wildcards' checkbox), so you can see what's going on and you
don't need to screw around with coding at all.



"Shannon Jacobs" wrote in message
...
Jezebel wrote:
You could try --

Sub clean()
Dim aRegExp as object
Dim I As Long

Set aRegExp = CreateObject("vbscript.regexp")
With aRegExp
.Pattern = "(height|width)=""12"" "
.Global = True
.IgnoreCase = True
' .MultiLine = True
End With

For I = 65 To ActiveSheet.UsedRange.Rows.Count
Cells(I, 1) = aRegExp.Replace(Cells(I, 1), "")
Next

End Sub

Are there more search strings you're dealing with? Otherwise, it
would be heaps quicker to use:

with Range("A65:A" & ActiveSheet.UsedRange.Rows.Count)
.Replace What:="Height=""12""", Replacement:="",
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
.Replace What:="Width=""12""", Replacement:="",
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
end with


This sounds very much like what I've been trying to figure out, though I
won't be able to pursue it farther until later, probably Friday at

earliest.
However, right now I'm struggling with the following problem. I want to

drop
the last part of the image tags, and all of the first part including the
directory information. All I want to have left is the stem name of the

file.
The first triplet works properly, but the second one is overmatching
somehow, and the stem name is also dropped. Even worse, in this latest

form
I tried to use the parenthetic grouping for a clause and then the \1 to
return the stem, but now it just sticks a "\1" into the file.

' remove ends of image tags
aRegExp.Pattern = "\.gif[\w+ ?= ?\w+]*"
theLine = aRegExp.Replace(theLine, "")

' remove fronts of image tags, but save the last word
aRegExp.Pattern = "<img src ?= ?\./[\w+/]*(\w+)"
theLine = aRegExp.Replace(theLine, "\1")

I haven't even gotten to the really nasty part of the file transformation
yet, though so far I'm getting a moderate feel for how they work here...

Not sure if it will help, but my current primary reference is the

following
URL, and I'm working with an English version of Excel XP right now.


http://msdn.microsoft.com/library/de...ting051099.asp