Increase speed of VBA Macro

CPGDeveloper

Board Regular
Joined
Oct 8, 2008
Messages
174
I have VBA code that's repeatedly going through a document changing one style to another...something like this:

'Remove StyleA
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("StyleA")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("StyleA1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

I have about a dozen of these changes to make in this macro and simply repeat the above code, just replacing the style names that I want changed. Currently, the code takes something like 20 seconds to run. Does anyone know of a way to combine all of them into a simpler and/or more efficient statement? Any tips would be greatly appreciated. Thanks.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
CPGDeveloper,

You could create an array to hold the styles.

Then cycle thru your displayed code, based on the array items.

What are all the styles/names?
 
Upvote 0
hiker95,

Thank you -- I'm a newbie with arrays -- my style names vary...everything from "LL-(Chr)--Primary If-Then (red) [BRACKETS]" to "119b char".

So would I do something like:

Dim MyStyle As String
MyStyle = Array("Style1", "Style2", "Style3", etc...)

And then replace the name of the style in the code with 'MyStyle'?

Apologies if this is a very basic question...Thanks for your help.
 
Upvote 0
CPGDeveloper,

Can we see all your macro code?

If posting VBA code, please use Code Tags, see below in my Signature block: If posting VBA code, please use Code Tags - like this
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,767
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top