How to modify VBA so the order of print out follows input print pages?

London12F

Board Regular
Joined
Jun 21, 2016
Messages
59
The VBA can do the following:
1. Input print pages (e.g. 3,6,4)
2 .VBA can print out as pdf all input pages, but not in order (i.e. 6,3,4).

How to modify the VBA so that the print out is in order of input pages, i.e. 3,6,4?

Code:
Sub PrintSOR()

On Error GoTo 1000


ro = ActiveCell.Row
co = ActiveCell.Column


Dim selectionRange As Range
Dim newRange As Range


PrintoutPath = "C:\Users\DK-01\Desktop\Scan\"


Set Sh = ActiveSheet
n = ActiveSheet.HPageBreaks.Count
    
which = InputBox("Print pages (1,3,5-" & n & ")?", PrintoutPath, "1-" & n)


NameOfFile = which
which = Split(which, ",")


For i = LBound(which) To UBound(which)
    part = Split(which(i), "-")
    f = part(0)
    If UBound(part) = 0 Then
        t = f
    Else: t = part(1)
    End If
    
GoSub SetRange


Next i


selectionRange.Select
    
Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=PrintoutPath & NameOfFile & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False


Cells(ro, co).Select: End


SetRange:


s = Range("print_area").Address()
P_Area = Split(s, ",")
p = Split(P_Area(0), ":")


PL = Mid(p(0), 2, InStrRev(p(0), "$") - 2) ' left of printarea = B
PR = Mid(p(1), 2, InStrRev(p(0), "$") - 2) ' right of printarea
    
   Set newRange = Range(Cells(55 * (f - 1) + 1, Asc(PL) - 64), Cells(55 * t, Asc(PR) - 64))
    
    If selectionRange Is Nothing Then
        Set selectionRange = newRange
    Else
        Set selectionRange = Application.Union(selectionRange, newRange)
    End If
Return


1000 End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
.

print specific worksheets
place sheet names in col d







Code:
Sub PrintAllSheets()
    'Assuming first sheet name is in cell D2:
    Dim c As Range
    
    'loop through all values in column D
    For Each c In Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row)
        Worksheets(c.Value).PrintOut
    Next c
End Sub
 
Upvote 0
[TABLE="width: 486"]
<colgroup><col></colgroup><tbody>[TR]
[TD]My main problem for this VBA is the order of printing,[/TD]
[/TR]
[TR]
[TD]My VBA can print all pages I specified (3,6,4) but not my expected order.[/TD]
[/TR]
[TR]
[TD](i.e. actual print pages is 6,3,4 not my expected 3,6,4)[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]It may be ideal if you can solve my problem of ordering in printing.[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]In practice I have to print ~50 out of 350 pages,[/TD]
[/TR]
[TR]
[TD]so I don't want to extract and copy 50 pages to another sheet to print.[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I thought you wanted sheets in a specific order, not pages from a single sheet.
Forget about this code if that is indeed the case.
I'll leave the code here just in case someone might be able to use it.

Code:
Sub Mix_Print_And_Back()
Dim shArr, shArr2, i As Long, ii As Long, j As Long

'Following are the sheets to be printed to PDF
shArr2 = Array("Sheet4", "Sheet7", "Sheet2", "Sheet5", "Sheet1", "Sheet10", "Sheet8", "Sheet3", "Sheet9", "Sheet6")

'Put the current order of the sheets in an array to be used later
Redim shArr(1 To Sheets.Count)
    For i = 1 To Sheets.Count
        shArr(i) = ThisWorkbook.Sheets(i).Name
    Next

'Put the sheets in the order they need to be printed
    For ii = 1 To Ubound(shArr2)
        Sheets(shArr2(ii)).Move After:=Sheets(shArr2(ii - 1))
    Next ii

'Select the sheets to be printed to PDF here. Print to PDF

'Put the sheets back in the order they were previously
    For j = 1 To Ubound(shArr)
        Sheets(shArr(j)).Move After:=Sheets(shArr(j - 1))
    Next j
End Sub
 
Last edited:
Upvote 0
[TABLE="width: 559"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD="colspan: 2"]Hi jolivanes, [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 6"]All pages to be printed are in the same sheet and page numbers have been assigned by excel after setting print range.[/TD]
[/TR]
[TR]
[TD="colspan: 5"]The VBA can print specified pages (e.g. P.3, P.6, P.4) as pdf, merely not in order.[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
The only way I know of is to hide all the rows that don't need to be printed, print visible cells, unhide, hide other rows to have the next page exposed, print, unhide etc etc.
Or as Mr Kenneth Hobson suggested. Should be a breeze when it's all setup properly.
 
Upvote 0
[TABLE="width: 504"]
<colgroup><col span="7"></colgroup><tbody>[TR]
[TD="colspan: 2"]Hi jolivanes,[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 7"]As there may be upto 50 pages or over, I don't want to hide/unhide etc over 50 times.[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 5"]All I want is to print out in order (by modifying my VBA).[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
This is one pdf per "page"?

Another method could be to make the pdfs and then combine to one pdf. Several programs can do combine pdfs. I have used Pdfcreate via va to do it.

Which method is faster, I am not sure without testing. My preference is first method.
 
Upvote 0
[TABLE="width: 366"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD="colspan: 2"]Hi Kenneth, [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 6"]My VBA can make several pages (e.g. pp. 3,6,4) into one pdf.[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 6"]My problem is only the order of printout. (i.e. pp. 3,6,4 not pp. 6,3,4)[/TD]
[/TR]
[TR]
[TD="colspan: 4"]It is ideal to modify my VBA .[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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