Print No. off page from cell.

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
72
Office Version
  1. 2016
Platform
  1. Windows
Hi, I use this VBA code to print a sheet with but it does not work. Now I want Excel to print the number of pages (A4) that are in cell E1.
All help will be appreciated.
Regards Klaus W
VBA Code:
Sub Lukaf_Rektangelafrundedehjørner1_Klik()

Dim intRaekke As Integer

Dim SidsteSide As Integer

SidsteSide = Sheets(intRaekke).Range("E1")

Sheets(intRaekke).PrintOut From:=1, To:=SidsteSide

End If

Next

End Sub
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
You need to initialize the variable 'intRaekke'. It has no value as the code is currently written and it should be producing a Subscript out of Range error when you run the code. I see a 'Next' at the bottom of the code but there is no 'For' at the top to make the loop. Looks like incomplete code.
 

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
72
Office Version
  1. 2016
Platform
  1. Windows
Thanks, but I don't now how to fix it. ;)
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
@KlausW , try ...

VBA Code:
Sub Lukaf_Rektangelafrundedehjørner1_Klik()

    Dim intRaekke As Integer
    Dim SidsteSide As Integer

    intRaekke = 1   ' Index number within the Sheets collection

    SidsteSide = Sheets(intRaekke).Range("E1")

    Sheets(intRaekke).PrintOut From:=1, To:=SidsteSide

End Sub
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

Thanks, but I don't now how to fix it. ;)
You can try the code offered by @GWteB. But it would help us to help you if you would explain a little more clearly if you want just one sheet printed, and which sheet it is, or if you want more than one sheet printed and which ones they are. I understand that you want multiple copies of whatever is printed, but then the code implies that you might have been trying to print more than one sheet. So please be clear in your objective.
 

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
72
Office Version
  1. 2016
Platform
  1. Windows
@KlausW , try ...

VBA Code:
Sub Lukaf_Rektangelafrundedehjørner1_Klik()

    Dim intRaekke As Integer
    Dim SidsteSide As Integer

    intRaekke = 1   ' Index number within the Sheets collection

    SidsteSide = Sheets(intRaekke).Range("E1")

    Sheets(intRaekke).PrintOut From:=1, To:=SidsteSide

End Sub
It do not work
 

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
72
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

It do not work
I will try to explain, in a sheet I want Excel to print the number that is in cell E1. In cell E1 there can number like 1,2,4, 6 or another number. That number in E1 is the number of pages (A4). For example, there are 3 in cell E1, Excel must print 3 (A4) pages. If it says 6, Excel must print 6 (A4) pages. An so on. Does it make sense. KW
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
Assuming that cell E1, in which is the number of the last page to be printed, is on Sheet1 and assuming that the pages to be printed (1 to [number in E1]) are also on Sheet1 the code below is likely to do what you want.

VBA Code:
Public Sub PrintSomePages()

    Dim oWs As Worksheet, n As Long

    Set oWs = ThisWorkbook.Sheets("Sheet1")

    n = CLng(oWs.Range("E1").Value)

    oWs.PrintOut From:=1, To:=n, Copies:=1

End Sub
 
Solution

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
72
Office Version
  1. 2016
Platform
  1. Windows
Assuming that cell E1, in which is the number of the last page to be printed, is on Sheet1 and assuming that the pages to be printed (1 to [number in E1]) are also on Sheet1 the code below is likely to do what you want.

VBA Code:
Public Sub PrintSomePages()

    Dim oWs As Worksheet, n As Long

    Set oWs = ThisWorkbook.Sheets("Sheet1")

    n = CLng(oWs.Range("E1").Value)

    oWs.PrintOut From:=1, To:=n, Copies:=1

End Sub
Thank I will try it, an return KW
 

jolivanes

Well-known Member
Joined
Sep 5, 2004
Messages
1,708
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
Are "Sheets" and "Pages" mixed up?
If I have a Sheet and set the printarea to A1:I260, I can do several things.
I can print "Sheet1" 5 times which results in 30 printed A4 size sheets of paper because the range A1:I260 has 6 pages.
Page1 is from A1 to I47, Page2 from A48 to I94, Page3 from A95 to I141 and so on.
But we can also print just one page any amount of times
As GWteB showed you: "PrintOut From:=1, To:=n, Copies:=1"
So if you made that to be
Code:
PrintOut From:=2, To:=2, Copies:=5
you would get 5 printed A4 pages from A48 - I94. All the same.
However, if your printarea is just a single page (like above example, from A1 to I47 or less) and you want multiple pages printed, it would be something like
Code:
PrintOut From:=1, To:=1, Copies:= Range("E1").Value
 

Watch MrExcel Video

Forum statistics

Threads
1,127,133
Messages
5,622,928
Members
415,941
Latest member
georgiana686

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
Top