VBA for printing in reverse order

nahean

New Member
Joined
Jan 6, 2022
Messages
15
Office Version
  1. 2007
Platform
  1. Windows
I am trying to print a page range in reverse order. I found a sample code but can not fit in my current worksheet.

Sub ReversePrint()

Dim xPages As Long
xPages = ExecuteExcel4Macro("GET.DOCUMENT(50)")
For xIndex = xPages To 1 Step -1
Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
Next
End Sub

Suppose I want to print page 14 to 21 but in reverse order. can anyone provide me a working code or adjust the above line.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This could be a solution:
VBA Code:
Option Explicit
Sub ReversePrint()
    Dim pagePrint As String
    Dim FirstLast() As String
    Dim xIndex As Long
    pagePrint = InputBox("Enter 'page range' to be printed." & vbLf & vbLf & "Always use format:" & vbLf & "first-first for single page," & vbLf & "first-last for multiple pages.", "Input Box Reverse Print")
    If pagePrint Like "?-?" Then
        FirstLast = Split(pagePrint, "-")
        For xIndex = FirstLast(1) To FirstLast(0) Step -1
            Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
        Next
    Else
        MsgBox "No page number entered! or wrong format!"
    End If
End Sub
 
Upvote 0
This could be a solution:
VBA Code:
Option Explicit
Sub ReversePrint()
    Dim pagePrint As String
    Dim FirstLast() As String
    Dim xIndex As Long
    pagePrint = InputBox("Enter 'page range' to be printed." & vbLf & vbLf & "Always use format:" & vbLf & "first-first for single page," & vbLf & "first-last for multiple pages.", "Input Box Reverse Print")
    If pagePrint Like "?-?" Then
        FirstLast = Split(pagePrint, "-")
        For xIndex = FirstLast(1) To FirstLast(0) Step -1
            Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
        Next
    Else
        MsgBox "No page number entered! or wrong format!"
    End If
End Sub

Thanx for your quick soultion

can you tell me where should i change?

"Enter 'page range' to be printed."
is replace with "14 To 21"

am i right??
 
Upvote 0
Do you mean that you will always print from 14 to 21 without need of asking ? or to you mean you prefer to be asked and write "14 to 21" instead of "14-21" ?
In the first case it could be:
VBA Code:
Option Explicit
Sub ReversePrintFixNumber()
    Dim firstPage As Integer
    Dim lastPage As Integer
    Dim xIndex As Long
    firstPage = 14
    lastPage = 21
    For xIndex = lastPage To firstPage Step -1
        Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
    Next
End Sub
while in the second case:
Code:
Option Explicit
Sub ReversePrintTo()
    Dim pagePrint As String
    Dim FirstLast() As String
    Dim xIndex As Long
    pagePrint = InputBox("Enter 'page range' to be printed." & vbLf & vbLf & "Always use format:" & vbLf & _
                "'first to first' for single page," & vbLf & "'first to last' for multiple pages.", "Input Box Reverse Print")
    If pagePrint Like "? to ?" Then
        FirstLast = Split(pagePrint, " to ")
        For xIndex = FirstLast(1) To FirstLast(0) Step -1
            Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
        Next
    Else
        MsgBox "No page number entered! or wrong format!"
    End If
End Sub
 
Upvote 0
Solution
Do you mean that you will always print from 14 to 21 without need of asking ? or to you mean you prefer to be asked and write "14 to 21" instead of "14-21" ?
In the first case it could be:
VBA Code:
Option Explicit
Sub ReversePrintFixNumber()
    Dim firstPage As Integer
    Dim lastPage As Integer
    Dim xIndex As Long
    firstPage = 14
    lastPage = 21
    For xIndex = lastPage To firstPage Step -1
        Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
    Next
End Sub
while in the second case:
Code:
Option Explicit
Sub ReversePrintTo()
    Dim pagePrint As String
    Dim FirstLast() As String
    Dim xIndex As Long
    pagePrint = InputBox("Enter 'page range' to be printed." & vbLf & vbLf & "Always use format:" & vbLf & _
                "'first to first' for single page," & vbLf & "'first to last' for multiple pages.", "Input Box Reverse Print")
    If pagePrint Like "? to ?" Then
        FirstLast = Split(pagePrint, " to ")
        For xIndex = FirstLast(1) To FirstLast(0) Step -1
            Application.ActiveSheet.PrintOut from:=xIndex, To:=xIndex
        Next
    Else
        MsgBox "No page number entered! or wrong format!"
    End If
End Sub


thanx a lot, i will try both and again thanx for your quick response.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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