Solved

Increment Excel cell every time a page prints

Posted on 2011-09-24
3
301 Views
Last Modified: 2012-05-12


Excel 2010 - My goal is to increment cell E9 by 1 (on my spreadsheet) for every page printed, so I wrapped the print command around a loop that increments cell E9 by 1 for every page I print. I get prompted when I run the macro for how many pages I want, then off it goes. I had to add a sleep to it as it was flooding my printers queue. Here is what I came up with:

Sub pri()
'
' pri Macro


xnum = InputBox("How many pages do you wish to print")
Range("e9").Activate
start_num = ActiveCell.Value
For i = start_num To xnum
    start_num = start_num + 1
    ActiveCell.Value = start_num
    Sleep 3000
Next i
End Sub

This macro works great, I put a 1 in cell E9, run the macro, tell it I want tp print 25 pages and off it goes, gives me 25 pages, and on each page cell E9 is one number larger. Perfect...HOWEVER, if the printer jams at say page 17, and I clear the jam and I put an 18 in cell E9, run the macro and tell it I want 8 pages, the macro will not produce any pages. I get no error, I just get nothing out of the printer. I know the jam is cleared as I can print from Word fine. The macro only seems to work if I start with a 1 in E9. If I try to start E9 with any other #, the macro produces no pages.

Any ideas experts?
thx...BobR
0
Comment
Question by:bobrossi56
3 Comments
 
LVL 50

Accepted Solution

by:
teylyn earned 500 total points
Comment Utility
Hello,

If you've already printed 18 pages, then start_num will be 18. Now you want to print 8 pages, so xnum will be 8. Therefore, the for/next loop will be

for i = 18 to 8

This does not work, of course.

Since you always want to print as many pages as specified by xnum, you need to start your for/next loop at 1 and make sure that E9 will be incremented, for example like this:

xnum = InputBox("How many pages do you wish to print")
For i = 1 To xnum
    Range("E9") = Range("E9").Value + 1
    Sleep 3000
Next i

Open in new window


cheers, teylyn

0
 

Author Closing Comment

by:bobrossi56
Comment Utility
Works like a charm....thx much...BobR
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
bobrossi56,

Glad to see you got your answer!  Just one further note: if you are ever tempted to use the Workbook's BeforePrint event for this, be very careful: that event fires for print preview, so you may not get the behavior you desire :)

Patrick
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now