#1
|
|||
|
|||
Shorten/refine recorded vba code
Hi, I am very new to word VBA. I have recorded some code that works perfectly but it just takes to long to run on some of my documents. I believe when a recording is done there is a lot of the code that is not necessary to run perfectly, being a novice I am not sure what to take out. It would be greatly appreciated if someone could help me. |
#2
|
||||
|
||||
You would need to post the code if you wanted help to speed it up.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Sorry I tought I had uploaded the code. Code is as follows:
Sub INSERT_GRID() ' ' INSERT_GRID Macro ' ' Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=5, _ NumRows:=25, AutoFitBehavior:=wdAutoFitFixed With Selection.Tables(1) .Style = "Table Grid" .ApplyStyleHeadingRows = True .ApplyStyleLastRow = False .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = False End With Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-8.5, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-15.9, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-22.95, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-22.95, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=148.85, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=141.75, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=127.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=113.4, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=106.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=92.15, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=78, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=70.9, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=63.8, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=56.7, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=49.65, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=42.55, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=35.45, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=28.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=28.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=148.85, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=141.75, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=134.7, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=127.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=127.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=154.4, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=163, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=177.2, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=184.25, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=191.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=198.45, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=205.55, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=212.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=219.7, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=226.8, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=226.8, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=154.4, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=163, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=170.1, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=177.2, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=184.25, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=191.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=198.45, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=205.55, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=212.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=212.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=148.85, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=154.45, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=163.05, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=170.1, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=177.2, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=184.3, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=191.4, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=198.45, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=205.55, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=212.65, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=212.65, RulerStyle:= _ wdAdjustNone With Selection.Tables(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderHorizontal) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderVertical) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = -603923969 End With With Selection.Tables(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderHorizontal) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderVertical) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Selection.Sections(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders .DistanceFrom = wdBorderDistanceFromPageEdge .AlwaysInFront = True .SurroundHeader = True .SurroundFooter = True .JoinBorders = False .DistanceFromTop = 5 .DistanceFromLeft = 5 .DistanceFromBottom = 5 .DistanceFromRight = 5 .Shadow = False .EnableFirstPageInSection = True .EnableOtherPagesInSection = True .ApplyPageBordersToAllSections End With End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = -603923969 End With With Selection.Tables(1) .TopPadding = CentimetersToPoints(0.45) .BottomPadding = CentimetersToPoints(0.45) .LeftPadding = CentimetersToPoints(0.19) .RightPadding = CentimetersToPoints(0.19) .SPACING = 0 .AllowPageBreaks = True .AllowAutoFit = True End With End Sub |
#4
|
||||
|
||||
Most of your code can be removed to speed it up.
You start by applying a Table Style. A lot of the later code can be removed if the Table style being applied has those attributes as part of the style. Since that is a one off step to define the table style, you don't even need that to be part of the macro. The many lines adjusting the width of the columns can be reduced by deleting all but the last line for each column. For example the only line you need here is the one in red Code:
Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=148.85, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=154.45, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=163.05, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=170.1, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=177.2, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=184.3, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=191.4, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=198.45, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=205.55, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=212.65, RulerStyle:= wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=212.65, RulerStyle:= wdAdjustNone
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Hi Andrew, ok great thanks for that. Also just wanted to find out about the borders if there is a way to refine those aswell?
|
#6
|
|||
|
|||
Hi Andrew, just tried it out and has given me a error (attached as image) below is the code hopefully I have done it right?
Sub INSERT_GRID() ' ' INSERT_GRID Macro ' ' Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=5, _ NumRows:=25, AutoFitBehavior:=wdAutoFitFixed With Selection.Tables(1) .Style = "Table Grid" .ApplyStyleHeadingRows = True .ApplyStyleLastRow = False .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = False End With Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-8.5, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-15.9, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-22.95, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Rows.SetLeftIndent leftIndent:=-22.95, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=28.35, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=127.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=226.8, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=212.6, RulerStyle:= _ wdAdjustNone Selection.Tables(1).Columns(5).SetWidth ColumnWidth:=212.65, RulerStyle:= _ wdAdjustNone With Selection.Tables(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderHorizontal) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderVertical) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = -603923969 End With With Selection.Tables(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderHorizontal) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With With .Borders(wdBorderVertical) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = -603923969 End With .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Selection.Sections(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = -603923969 End With With .Borders .DistanceFrom = wdBorderDistanceFromPageEdge .AlwaysInFront = True .SurroundHeader = True .SurroundFooter = True .JoinBorders = False .DistanceFromTop = 5 .DistanceFromLeft = 5 .DistanceFromBottom = 5 .DistanceFromRight = 5 .Shadow = False .EnableFirstPageInSection = True .EnableOtherPagesInSection = True .ApplyPageBordersToAllSections End With End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = -603923969 End With With Selection.Tables(1) .TopPadding = CentimetersToPoints(0.45) .BottomPadding = CentimetersToPoints(0.45) .LeftPadding = CentimetersToPoints(0.19) .RightPadding = CentimetersToPoints(0.19) .SPACING = 0 .AllowPageBreaks = True .AllowAutoFit = True End With End Sub |
#7
|
|||
|
|||
Hi Andrew, ignore my last post. I forgot to select my text, its working fine with no error. Just would like to know if the borders part of code can be refined? Thanks in advance.
|
#8
|
|||
|
|||
If you add your own table style to the document template instead of using Table Grid you can set all the properties just by applying the style.
You can shorten your code by deleting all of the duplicate lines. Properties only need to be set once. |
#9
|
|||
|
|||
Hi Italophile, I am converting text into the table, so the table will vary in length depending how much text there is. So can I still do that by setting up my own table style? sorry very stupid when it comes to things like this.
|
#10
|
|||
|
|||
The size of the table is irrelevant.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to shorten English words of specific length by macro? | Emby | Word VBA | 4 | 10-16-2022 11:25 PM |
Shorten multi-year timeline | Tyler V | Visio | 0 | 06-14-2022 08:35 AM |
From recorded tro rational | Ulodesk | Word VBA | 4 | 02-07-2022 06:16 AM |
How to shorten Outlook server connection timeouts? | mmo | Outlook | 0 | 12-12-2015 03:33 PM |
Shorten/delete auto-converted dotted horizontal line? | pstein | Word | 3 | 09-12-2012 01:05 AM |