PDA

View Full Version : using a macro to insert a space between a number and a unit



rlaumen
12-16-2007, 05:47 AM
Hi,

(This is my first post, so please bear with me and take into account that I am not a VBA guru - otherwise I could have come up with something myself ;) )

I would like to create a macro that inserts a space between a number and a unit of measure (e.g. 64MB should become 64 MB, etc.).

I've got:
=============================
Sub InsertSpaceBetweenNumberAndUnit()
'
' InsertSpaceBetweenNumberAndUnit Macro
'
' This one is for MB
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([0-9])(MB)"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

' This one is for GB
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([0-9])(GB)"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

End sub
=============================

But if I need to add more units in addition to MB and GB, the code will become very long. I will probably need a short code containing an array or something.

Please note that I do not need something that simple puts a space between any number and any letter if there is none, as that would change product numbers etc. as well, and I don't want that.

Any ideas?

TIA,
Rob

TonyJollans
12-16-2007, 06:10 AM
Something like this?

UnitNames = Array("MB", "GB")

For ndx = LBound(UnitNames) To UBound(UnitNames)
ActiveDocument.Content.Find.Execute _
FindText:="([0-9]@)(" & UnitNames(ndx) & ")", _
ReplaceWith:="\1 \2", _
MatchWildcards:=True, _
Replace:=wdReplaceAll
Next

rlaumen
12-16-2007, 06:26 AM
Something like this?

Great, thanks, Tony!