Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-29-2014, 09:25 AM
rsrasc rsrasc is offline Need Help with Below Code Windows 7 64bit Need Help with Below Code Office 2010 64bit
Competent Performer
Need Help with Below Code
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default Need Help with Below Code

I need some help with the below code. I would like to automate this macro to change numbers with the following format:

0001 to 1.
0002 to 2.

0750 to 750.

Up to maybe 0999

Thanks!!!!


Sub ConvertingNumbers()
'
' ConvertingNumbers Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "0001"
.Replacement.Text = "1."


.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "0002"
.Replacement.Text = "2."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub
Reply With Quote
  #2  
Old 03-29-2014, 01:55 PM
macropod's Avatar
macropod macropod is offline Need Help with Below Code Windows 7 32bit Need Help with Below Code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't need a macro. You can use a wildcard Find/Replace, where:
Find = ([!1-9])[0]{1,}([0-9]{1,})
Replace = \1\2
Of course, you could encode that in a macro like the one I provided yesterday.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-30-2014, 05:51 AM
rsrasc rsrasc is offline Need Help with Below Code Windows 7 64bit Need Help with Below Code Office 2010 64bit
Competent Performer
Need Help with Below Code
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default Can't get it right?

My preference would be to encode in a macro because I need it for approximately 60 documents that I'm editing.

Anyway, I tested but doesn't work. Would you please show me what I did wrong?

Thanks in advance!!
Cheers!


Code:
Sub Demo2()
Application.ScreenUpdating = False
With ActiveDocument.Range
  .InsertBefore " "
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Text = ".([0-9]{1,3}.)"
    .Replacement.Text = ". \1"
    .Execute Replace:=wdReplaceAll
    .Text = " ([a-e].)"
    .Replacement.Text = "^p\1"
    .Execute Replace:=wdReplaceAll
    .Replacement.Style = "Strong"
    .Text = " ([!1-9])[0]{1,}([0-9]{1,})"
    .Execute Replace:=wdReplaceAll
  End With
  .Characters.First.Delete
End With
Application.ScreenUpdating = True
End Sub

Last edited by macropod; 03-30-2014 at 08:56 AM. Reason: Added code tags & formatting
Reply With Quote
  #4  
Old 03-30-2014, 09:05 AM
macropod's Avatar
macropod macropod is offline Need Help with Below Code Windows 7 32bit Need Help with Below Code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Well, apart from the fact you deleted an existing expression in that macro, you added a space to the start of the new Find expression, omitted a new Replace expression, which means the preceding character would be deleted and a paragraph inserted in its place and, by putting it after the code that applies the 'Strong' Style, would turn the replaced output bold. Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Text = "([!1-9])[0]{1,}([0-9]{1,})"
    .Replacement.Text = "\1\2"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
or, to integrate it into yesterday's macro:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  .InsertBefore " "
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Text = "([!1-9])[0]{1,}([0-9]{1,})"
    .Replacement.Text = "\1\2"
    .Execute Replace:=wdReplaceAll
    .Text = ".([0-9]{1,3}.)"
    .Replacement.Text = ". \1"
    .Execute Replace:=wdReplaceAll
    .Text = " ([a-e].)"
    .Replacement.Text = "^p\1"
    .Execute Replace:=wdReplaceAll
    .Replacement.Style = "Strong"
    .Text = " ([0-9]{1,3}.)"
    .Execute Replace:=wdReplaceAll
  End With
  .Characters.First.Delete
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-30-2014, 10:57 AM
rsrasc rsrasc is offline Need Help with Below Code Windows 7 64bit Need Help with Below Code Office 2010 64bit
Competent Performer
Need Help with Below Code
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default Thanks!

The code is working as intented.

Thank you for your support.
Reply With Quote
  #6  
Old 04-01-2014, 11:07 AM
rsrasc rsrasc is offline Need Help with Below Code Windows 7 64bit Need Help with Below Code Office 2010 64bit
Competent Performer
Need Help with Below Code
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default

Hello Macropod

Even when this thread has been solved, I would like to ask you for two things:

1. Would it be possible to modify the code so I could have more spaces between the questions--probably about six or seven spaces (between letter d and the next question)?

2. I was looking for the "Go Advanced" tab and couldn't find it.--I know, I know- I searched for but I'm not that lucky!!

Thanks anyway for your support!
Reply With Quote
  #7  
Old 04-01-2014, 03:42 PM
macropod's Avatar
macropod macropod is offline Need Help with Below Code Windows 7 32bit Need Help with Below Code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by rsrasc View Post
1. Would it be possible to modify the code so I could have more spaces between the questions--probably about six or seven spaces (between letter d and the next question)?
The spacing should be handled via Style definitions, with the appropriate before/after paragraph spacing, not by inserting empty paragraphs.
Quote:
2. I was looking for the "Go Advanced" tab and couldn't find it.--I know, I know- I searched for but I'm not that lucky!!
Try scrolling the reply window down...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform Code not quite right - help please vbanovice Word VBA 1 09-29-2013 09:20 PM
outlook vba code help HxG Outlook 0 09-20-2012 10:42 PM
Need Help with Below Code Where does my code go? rbaldwin Word VBA 3 03-14-2012 02:31 PM
Help with VBA Code Modification OTPM Excel Programming 0 09-16-2011 07:10 AM
Need Help with Below Code vbc code rajpeter Excel Programming 2 09-13-2011 02:29 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:34 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft