advance find and replace

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I would like to replace each .0 value with a 0.0. I have tried a find by
using the following wildcard .([0-9]{1}) but it pick up numbers like 15.2
etc. I only need to to add a zero before those numbers that are less than one.
ANy help would be appreciated.
 
Hi all,

I would like to replace each .0 value with a 0.0. I have tried a find by
using the following wildcard .([0-9]{1}) but it pick up numbers like 15.2
etc. I only need to to add a zero before those numbers that are less than one.
ANy help would be appreciated.

Change the find expression to

([!0-9])(.[0-9])

and use the replace expression

\10\2

The part of the find expression in the first pair of brackets, using
the ! operator to mean NOT, says "any character that is NOT a digit".
The {1} in your expression isn't necessary, since [0-9] already means
"any one digit". So the whole thing says "any character that is not a
digit, followed by a dot and any digit".

The replacement says "the character that matches the first set of
brackets, then a zero, then the characters that match the second set
of brackets".

This replacement will miss an occurrence at the very start of the
document, since there isn't any non-digit character preceding the dot.
That case is easy to fix manually, though.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Thank you for your help! I tried it and it works great. I did forget to
mention (I apologize) that the values I am trying to replace are in a table
and when I use the expression for that purpose it does not work. Perhaps I am
doing something wrong (I am new to this). If you could follow up with me one
more time I would greatly appreciate it.
Thanks a lot again,
Giusi

Jay Freedman said:
Hi all,

I would like to replace each .0 value with a 0.0. I have tried a find by
using the following wildcard .([0-9]{1}) but it pick up numbers like 15.2
etc. I only need to to add a zero before those numbers that are less than one.
ANy help would be appreciated.

Change the find expression to

([!0-9])(.[0-9])

and use the replace expression

\10\2

The part of the find expression in the first pair of brackets, using
the ! operator to mean NOT, says "any character that is NOT a digit".
The {1} in your expression isn't necessary, since [0-9] already means
"any one digit". So the whole thing says "any character that is not a
digit, followed by a dot and any digit".

The replacement says "the character that matches the first set of
brackets, then a zero, then the characters that match the second set
of brackets".

This replacement will miss an occurrence at the very start of the
document, since there isn't any non-digit character preceding the dot.
That case is easy to fix manually, though.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
I think the problem is similiar to what Jay decribes wrt the very start
of the document. In an individual cell there is nothing preceeding the
dot.

Try:
Sub ScratchMacro()
Dim myRng As Range
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
Set myRng = oCell.Range
myRng.End = myRng.End - 1
If myRng.Text = ".0" Then
myRng.Text = "0.0"
End If
Next
End Sub

Jay,

Since I know you will be back and see this, why does the following not
work? I don't understand why the line oCell.Range.End =
oCell.Range.End fails to decrease the range by 1 character:

Sub ScratchMacro2()
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
oCell.Range.End = oCell.Range.End - 1 'This line doesn't cause the
range end value to decrease
If oCell.Range.Text = ".0" Then
oCell.Range.Text = "0.0"
End If
Next
End Sub
 
Oops.

I meant I don't understand why the line oCell.Range.End =
oCell.Range.End - 1 fails to decrease the range by 1 character:
 
Hmm,

I am starting to convince myself that the issue is that a physical
cells range is what it is and can't be modified. This little bit of
code also fails:
Sub ScratchMacro2()
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
oCell.Range.MoveEnd wdCharacter, -1
oCell.Range.Select 'Selects the whole cell :-(
If oCell.Range.Text = ".0" Then
oCell.Range.Text = "0.0"
End If
Next
End Sub
 
Hmm,

I am starting to convince myself that the issue is that a physical
cells range is what it is and can't be modified. This little bit of
code also fails:
Sub ScratchMacro2()
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
oCell.Range.MoveEnd wdCharacter, -1
oCell.Range.Select 'Selects the whole cell :-(
If oCell.Range.Text = ".0" Then
oCell.Range.Text = "0.0"
End If
Next
End Sub

Hi Greg,

Yes, that's it exactly. You can't modify the .Start and .End of the
ranges of actual objects in the document, such as a cell or a
paragraph or the whole ActiveDocument. What you can do, though, is
assign that range to a Range object you declare, and then tinker with
that:

Sub ScratchMacro3()
Dim oCell As Cell
Dim oRg As Range
For Each oCell In Selection.Tables(1).Range.Cells
Set oRg = oCell.Range
oRg.MoveEnd wdCharacter, -1
If oRg.Text = ".0" Then
oRg.Text = "0.0"
End If
Next
Set oRg = Nothing
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Yes Jay, that is pretty much what I did in posting a possible solution
to the OPs question earlier. It was while working that out that I was
perplexed by not being able to simple manipulate the cell range.
Thanks for confirming my thoughts.
 
Thanks for all your help! The macro worked just fine. I have one more
question: can it be generalized to other numbers e.g., .1; .2; .256; .098
etc.? Again, I am new to both macros and wildcards and I don't know how to
change them myself.
Sincerely,
Giusi
 
Giusi,

We have abandoned "wildcards" ;-)

Try:

Sub ScratchMacro()
Dim myRng As Range
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
Set myRng = oCell.Range
myRng.End = myRng.End - 1
If Left(myRng.Text, 1) = "." Then
myRng.InsertBefore "0"
End If
Next
End Sub
 
Back
Top