Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default adding a page number in a cell

i have found a little routine that almost does what i want he

http://www.microsoft.com/office/comm...xp=&sloc=en-us

but what i need is for it to print one page and then the next page after
updating that cell. here is the code that i borrowed from that thread:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim AnySheet As Worksheet
Dim myRange As Range
Dim CellRange As Object

For Each AnySheet In ActiveWindow.SelectedSheets
'change "Sheet1" to actual sheet name
If AnySheet.Name = "Sheet1" Then
Set myRange = AnySheet.Range("I7")
For Each CellRange In myRange
CellRange.Value = CellRange.Value + 1
Next
End If
Next
'delete the next line to
'actually let it print out
'this is here just for testing
'Cancel = True
End Sub

it works in incrementing the cell, but it doesnt print the first page then
update the cell and print the second.

basically i need this for an invoice where the page number is located in the
middle of the page rather than in the header or footer.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default adding a page number in a cell

<<i have found a little routine that almost does what i want he
Your link refers to this post, as far as I can tell!

Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default adding a page number in a cell

dang


i dont have multiple sheets. i have one sheet and i need the number to
increment after printing the first page. i have told the sheet to repeat rows
at the top so that the header information will be printed on both pages. now
in the future there may be more than 2 pages so any solution needs to have
the ability to increment print the page and then increment and print the page.

i will try out your mod to the code, but something tells me i didnt explain
myself well enough the first time.

"Bill Renaud" wrote:

<<i have found a little routine that almost does what i want he
Your link refers to this post, as far as I can tell!

Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default adding a page number in a cell

<<i dont have multiple sheets. i have one sheet ...

This conflicts with your originally posted code. In it, you had "For Each
AnySheet In ActiveWindow.SelectedSheets".

<<i will try out your mod to the code, but something tells me i didnt
explain myself well enough the first time.

No need to try out the code, as it won't do what you want.

Tell us what you have for print headers (how many rows at the top of the
worksheet). This is now a more complex problem. Why not leave the Page
Numbers in the Header, instead of putting them in the printable area of the
document?

--
Regards,
Bill Renaud



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default adding a page number in a cell

Hello Bill

thanks for responding.

to quote myself "basically i need this for an invoice where the page number
is located in the middle of the page rather than in the header or footer."

i am trying to somewhat simplify our out of town invoice creation
spreadsheet. currently creating an invoice means duplicating the top half of
the first spreadsheet a number of times, after you figure out how many lines
you have that are past the bottom of the first printable page.

i figured if i was to just increment that cell when repeating rows and
printing then i could get it to be all on one sheet and still have multiple
pages.

cell I7 is the cell where i want it to cause the number to increase for each
page it will print. each page being what is created when the user adds line
items to the parts sold area.

like i had said i borrowed that code, i hadnt even modified it.

i am repeating 23 rows at the top of each page, and the cell i need to
increment is on row 7, inside of the repeating section.

any ideas and help is appreciated.

"Bill Renaud" wrote:

<<i dont have multiple sheets. i have one sheet ...

This conflicts with your originally posted code. In it, you had "For Each
AnySheet In ActiveWindow.SelectedSheets".

<<i will try out your mod to the code, but something tells me i didnt
explain myself well enough the first time.

No need to try out the code, as it won't do what you want.

Tell us what you have for print headers (how many rows at the top of the
worksheet). This is now a more complex problem. Why not leave the Page
Numbers in the Header, instead of putting them in the printable area of the
document?

--
Regards,
Bill Renaud






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default adding a page number in a cell (Print Multiple Page Worksheet with Page Number in a Cell)

Try this routine. You can put it in a standard code module either in the
invoice workbook, or in a separate workbook. Call this routine from a
button somewhere or from the Tools|Macro|Macros command. It will put the
current page number in cell I7 before it prints each page. Make sure that
you have "Rows to repeat at top" in Page Setup correctly set to have your
common rows on the worksheet printed at the top of each printed page.

Public Sub PrintInvoice()
Dim wsActive As Worksheet
Dim rngPageNumber As Range
Dim lngPage As Long

Set wsActive = ActiveSheet
Set rngPageNumber = wsActive.Range("I7")

For lngPage = 1 To wsActive.HPageBreaks.Count + 1
rngPageNumber.Value = lngPage
wsActive.PrintOut From:=lngPage, _
To:=lngPage, _
Copies:=1
Next lngPage
End Sub

--
Regards,
Bill Renaud



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default adding a page number in a cell

How could you get it to work if some worksheets are larger than 1 printed
page in size?

"Bill Renaud" wrote:


Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default adding a page number in a cell

figured it out... Had to modify part of what you said above, but it seems to
work.

Dim ws As Worksheet
Dim lngPageNumber As Long
Dim hPgBrks As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
hPgBrks = 0
If ws.HPageBreaks.Count = 0 Then
hPgBrks = 1
Else
hPgBrks = ws.HPageBreaks.Count + 1
End If
lngPageNumber = lngPageNumber + hPgBrks
ws.Range("Z1").Value = lngPageNumber
Next ws

Thanks for the great posts that helped me figure it out "on my own".

Ben

"Ben Langhammer" wrote:

How could you get it to work if some worksheets are larger than 1 printed
page in size?

"Bill Renaud" wrote:


Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default adding a page number in a cell


I have been trying to figure out, how to increase a number in a cell each
time you print it, for sometime! Now for the stupid question...Where do i
place this code?

Thanks in advance!
Barbara


"Ben Langhammer" wrote:

figured it out... Had to modify part of what you said above, but it seems to
work.

Dim ws As Worksheet
Dim lngPageNumber As Long
Dim hPgBrks As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
hPgBrks = 0
If ws.HPageBreaks.Count = 0 Then
hPgBrks = 1
Else
hPgBrks = ws.HPageBreaks.Count + 1
End If
lngPageNumber = lngPageNumber + hPgBrks
ws.Range("Z1").Value = lngPageNumber
Next ws

Thanks for the great posts that helped me figure it out "on my own".

Ben

"Ben Langhammer" wrote:

How could you get it to work if some worksheets are larger than 1 printed
page in size?

"Bill Renaud" wrote:


Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default adding a page number in a cell

You place the code in the visual basic part of the excel file. If you go to
the "Developer" tab in the ribbon, there should be an option for "Visual
Basic" (it is the left most button I think on the default ribbon for Excel
2007). Once in there, you want to put the code in the "ThisWorkbook"
section....

I hope that makes a bit of sense.

Ben

"Babs" wrote:


I have been trying to figure out, how to increase a number in a cell each
time you print it, for sometime! Now for the stupid question...Where do i
place this code?

Thanks in advance!
Barbara


"Ben Langhammer" wrote:

figured it out... Had to modify part of what you said above, but it seems to
work.

Dim ws As Worksheet
Dim lngPageNumber As Long
Dim hPgBrks As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
hPgBrks = 0
If ws.HPageBreaks.Count = 0 Then
hPgBrks = 1
Else
hPgBrks = ws.HPageBreaks.Count + 1
End If
lngPageNumber = lngPageNumber + hPgBrks
ws.Range("Z1").Value = lngPageNumber
Next ws

Thanks for the great posts that helped me figure it out "on my own".

Ben

"Ben Langhammer" wrote:

How could you get it to work if some worksheets are larger than 1 printed
page in size?

"Bill Renaud" wrote:


Anyway, maybe you mean to use something like the following. It puts a page
number in cell I7 on each of the selected worksheets starting at (page) 1.
This assumes that each worksheet is only 1 printed page in size.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
Dim lngPageNumber As Long

lngPageNumber = 0

For Each ws In ActiveWindow.SelectedSheets
lngPageNumber = lngPageNumber + 1
ws.Range("I7").Value = lngPageNumber
Next ws
End Sub

--
Regards,
Bill Renaud



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display current page number in any cell of that page. Laljeet Excel Discussion (Misc queries) 8 February 2nd 08 01:31 AM
sheet tabs as page number and in a cell page of pages? [email protected] Excel Discussion (Misc queries) 0 November 22nd 05 02:43 PM
macro adding a number to a number already in a cell Tom Ogilvy Excel Programming 0 October 18th 03 04:34 PM
macro adding a number to a number already in a cell Don Guillett[_4_] Excel Programming 0 October 17th 03 05:21 PM
macro adding a number to a number already in a cell Ron de Bruin Excel Programming 0 October 17th 03 04:59 PM


All times are GMT +1. The time now is 12:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"