save excel sheets as pdf via vba

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
741
Office Version
  1. 365
  2. 2010
i need to re-arrange the tabs so that it saves the PDF in that order....is there way to do this without manually moving the worksheets?

i tried re-arranging the sheet names in the vba array code, but that didnt seem to do the trickl
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
.
Code:
[COLOR=#00007F][FONT=&quot]Sub[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] Reorder_Sheets()[/FONT][/COLOR]

[COLOR=#007F00][FONT=&quot]'PURPOSE: Order Worksheets in a custom way (works even if some of the worksheets are missing)[/FONT][/COLOR]
[COLOR=#007F00][FONT=&quot]'SOURCE: www.TheSpreadsheetGuru.com[/FONT][/COLOR]

[COLOR=#00007F][FONT=&quot]Dim[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] x [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]As[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Long[/FONT][/COLOR]
[COLOR=#00007F][FONT=&quot]Dim[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] myOrder [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]As[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Variant[/FONT][/COLOR]

[COLOR=#403F41][FONT=&quot]Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]False[/FONT][/COLOR]
[COLOR=#403F41][FONT=&quot]Application.ScreenUpdating = [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]False[/FONT][/COLOR]

[COLOR=#403F41][FONT=&quot]myOrder = Array("Sheet1", "Sheet4", "Sheet3", "Sheet6", "Sheet9", "Sheet11", "Sheet10", "Sheet5")[/FONT][/COLOR]

[COLOR=#00007F][FONT=&quot]On[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Error[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Resume[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Next[/FONT][/COLOR]
[COLOR=#403F41][FONT=&quot]  [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]For[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] x = [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]UBound[/FONT][/COLOR][COLOR=#403F41][FONT=&quot](myOrder) [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]To[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]LBound[/FONT][/COLOR][COLOR=#403F41][FONT=&quot](myOrder) [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Step[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] -1[/FONT][/COLOR]
[COLOR=#403F41][FONT=&quot]    Worksheets(myOrder(x)).Move Before:=Worksheets(1)[/FONT][/COLOR]
[COLOR=#403F41][FONT=&quot]  [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Next[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] x[/FONT][/COLOR]
[COLOR=#00007F][FONT=&quot]On[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Error[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]GoTo[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] 0[/FONT][/COLOR]

[COLOR=#403F41][FONT=&quot]Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]True[/FONT][/COLOR]
[COLOR=#403F41][FONT=&quot]Application.ScreenUpdating = [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]True[/FONT][/COLOR]

[COLOR=#00007F][FONT=&quot]End[/FONT][/COLOR][COLOR=#403F41][FONT=&quot] [/FONT][/COLOR][COLOR=#00007F][FONT=&quot]Sub[/FONT][/COLOR]

Arrange sheets in order desired in the ARRAY statement. Save the workbook. Then convert to PDF.
Re-arrange and save afterwards if desired.

https://www.thespreadsheetguru.com/the-code-vault/2014/4/7/custom-order-of-worksheet-positions
 
Upvote 0
.
Code:
[COLOR=#00007F][FONT='inherit']Sub[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] Reorder_Sheets()[/FONT][/COLOR]

[COLOR=#007F00][FONT='inherit']'PURPOSE: Order Worksheets in a custom way (works even if some of the worksheets are missing)[/FONT][/COLOR]
[COLOR=#007F00][FONT='inherit']'SOURCE: www.TheSpreadsheetGuru.com[/FONT][/COLOR]

[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] x [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Long[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] myOrder [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Variant[/FONT][/COLOR]

[COLOR=#403F41][FONT='inherit']Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']False[/FONT][/COLOR]
[COLOR=#403F41][FONT='inherit']Application.ScreenUpdating = [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']False[/FONT][/COLOR]

[COLOR=#403F41][FONT='inherit']myOrder = Array("Sheet1", "Sheet4", "Sheet3", "Sheet6", "Sheet9", "Sheet11", "Sheet10", "Sheet5")[/FONT][/COLOR]

[COLOR=#00007F][FONT='inherit']On[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Error[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Resume[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Next[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']For[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] x = [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']UBound[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'](myOrder) [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']To[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']LBound[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'](myOrder) [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Step[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] -1[/FONT][/COLOR]
[COLOR=#403F41][FONT='inherit']    Worksheets(myOrder(x)).Move Before:=Worksheets(1)[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Next[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] x[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']On[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Error[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']GoTo[/FONT][/COLOR][COLOR=#403F41][FONT='inherit'] 0[/FONT][/COLOR]

[COLOR=#403F41][FONT='inherit']Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']True[/FONT][/COLOR]
[COLOR=#403F41][FONT='inherit']Application.ScreenUpdating = [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']True[/FONT][/COLOR]

[COLOR=#00007F][FONT='inherit']End[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Sub[/FONT][/COLOR]

Arrange sheets in order desired in the ARRAY statement. Save the workbook. Then convert to PDF.
Re-arrange and save afterwards if desired.

https://www.thespreadsheetguru.com/the-code-vault/2014/4/7/custom-order-of-worksheet-positions


excellent, that worked..very creative.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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