Printing issues using macros and loops

rugbydud

New Member
Joined
Jun 19, 2018
Messages
4
I need help with a project. I set up a workbook to track production data and am having trouble with the printing code. The primary worksheet (employeedata) pulls data from a txt file based on an operator code that you enter into a cell. Upon entering, the worksheet refreshes with the data corresponding to the operator code. This is done by using an array to cycle through the txt file and displaying each instance.

I am trying to create a Print All button to cycle through each operator in the table, automatically enter it into the cell and print the page. here is my code so far. It will show the first sheet fine but will go into an endless loop trying to print. I have a button that will print each individually, but that would be very time consuming.


Code:
Sub PrintAll()     ' PrintAll Macro
     ' Select cell A2, *first line of data*.
     Application.ScreenUpdating = False
     Worksheets("Employees").Activate
     ActiveSheet.Range("A2").Select
     
     ' Set Do loop to stop when an empty cell is reached.
     Do Until IsEmpty(ActiveCell)
          'Copy the data
          Sheets("Employees").Range("a2").Copy
          
          'Activate the destination worksheet
          Sheets("Employeedata").Activate
        
          'Select the target range
          Range("a2").Select
        
          'Paste in the target destination
          Worksheets("EmployeeData").Range("$a$2").Select
          ActiveSheet.Paste
          Application.SendKeys "{ENTER}"
          Worksheets("EmployeeData").PrintOut From:=1, TO:=2, Preview:=True, ignoreprintareas:=false
        
          ' Step down 1 row from present location.
         Worksheets("Employees").Activate
         ActiveCell.Offset(1, 0).Select
     Loop  
End Sub
 
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I would have thought it is the Do Until that isn't exiting correctly then... are the cells empty or are there formula in them? If the cells contain formulas then the code will carry on. You may be better using

Do Until ActiveCell.value = ""
 
Upvote 0
Try this.
Code:
Sub PrintAll()
Dim wsData As Worksheet
Dim wsPrint As Worksheet
Dim rngData As Range
     
     ' Select cell A2, *first line of data*.
     Application.ScreenUpdating = False
     
     Set wsData = Worksheets("Employees")
     Set wsPrint = Worksheets("EmployeeData")
     
     Set rngdsta = wsData.Range("A2")
     ActiveSheet.Range("A2").Select
     
     ' Set Do loop to stop when an empty cell is reached.
     Do Until rngData.Value = ""
          'Copy the data
          
          rngData.Copy wsPrint.Range("A2")
          
          ' print sheet
          wsData.PrintOut From:=1, TO:=2, Preview:=True, ignoreprintareas:=False
          
        ' move to next employee
        Set rngData = rngData.Offset(1)
        
     Loop
     
End Sub
 
Upvote 0
Thank you for the response, it worked well. Would this code also work for copy/paste as well? Same idea of cycling through, just that it copies a range and pastes it to another tab labeled using the same list versus it printing.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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