VBA to sequentially print upon single command

cool0masty2

New Member
Joined
Oct 11, 2013
Messages
20
I have this workbook where I paste data to, from an online source. Once the data is copied in the "DATA" Sheet, C2 is copied and pasted into "AWB"!R4, a print command is then sent. This goes on for C3, C4 ... until the next cell is empty.
I have automated this task a little. but what I really am looking for is to
1. Check the next row (suppose C5), if it's empty, then end/break the cycle.
2. Simplify the code using LOOP, as it will surely make the module lighter.

The workbook can be found here: DRTVs' AWB. Please download it and use Excel for any changes.

I can allow folder access so you can upload a new version of it. Let me know.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
VBA Code:
Sub PrintLabels()

    Dim lLastDataRow As Long
    Dim lIndex As Long
    
    With Worksheets("AWB")
        .Select
        .PageSetup.PrintArea = Range("A1:N29")
    End With
    
    With Worksheets("Data")
        lLastDataRow = .Cells(.Rows.Count, 3).End(xlUp).Row
        For lIndex = 2 To lLastDataRow
            .Cells(lIndex, 3).Copy Destination:=Worksheets("AWB").Range("R4")
            ActiveSheet.PrintOut Copies:=1
        Next
    End With
    
End Sub
 
Upvote 0
VBA Code:
Sub PrintLabels()

    Dim lLastDataRow As Long
    Dim lIndex As Long
  
    With Worksheets("AWB")
        .Select
        .PageSetup.PrintArea = Range("A1:N29")
    End With
  
    With Worksheets("Data")
        lLastDataRow = .Cells(.Rows.Count, 3).End(xlUp).Row
        For lIndex = 2 To lLastDataRow
            .Cells(lIndex, 3).Copy Destination:=Worksheets("AWB").Range("R4")
            ActiveSheet.PrintOut Copies:=1
        Next
    End With
  
End Sub
Thanks for your response.

I already made it work using the code I have copied below.
It's working exactly how I wanted it to be done.
Please take a look and let me know if you see any flaws in it?
VBA Code:
Sub Print_AWB()
'
' Print_AWB Macro
' Prints AWB automatically.
'
' Keyboard Shortcut: Ctrl+Shift+B
'


i = 2
Do Until IsEmpty(Cells(i, 3))

        Worksheets("DATA").Select
Cells(R + i, 3).Select
Selection.Copy
    ActiveSheet.Next.Select
    ActiveSheet.Range("R4").Select
    ActiveSheet.Paste
        Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
ActiveSheet.Previous.Select


i = i + 1
Loop
End Sub

I have no understanding of VBA, but I somehow managed and wrote it on my own with little help from Macro Recorder, and Youtube.
 
Upvote 0
Worksheets("DATA").Select
should be before
Do Until IsEmpty(Cells(i, 3))
otherwise the Do Until may be looking at the wrong sheet

These 2 lines
ActiveSheet.Range("R4").Select
ActiveSheet.Paste

may be combined into
ActiveSheet.Range("R4").Paste

Otherwise if it works stay with it.
 
Upvote 0
This is what I have in the updated version now:
VBA Code:
i = 2
Worksheets("DATA").Select
Do Until IsEmpty(Cells(i, 3))

        
Cells(R + i, 3).Select
Selection.Copy
    ActiveSheet.Next.Select
    Range("R4").PasteSpecial
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    ActiveSheet.Previous.Select


i = i + 1
Loop

Couldn't find any .Paste method, so I used .PasteSpecial, it works.

Updated the code as you suggested, found it to be more flawless.
 
Upvote 0
I was wrong with my earlier advice. (Did it from memory without checking). The following is tested:

You can replace

Code:
Cells(R + I, 3).Select
Selection.Copy
    ActiveSheet.Next.Select
    Range("R4").PasteSpecial
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    ActiveSheet.Previous.Select


I = I + 1

With

Code:
    Cells(R + I, 3).Copy Destination:=ActiveSheet.Next.Range("R4")
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    I = I + 1


The PasteSpecial command has arguments, for example:
Code:
    Range("F19").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
 
Upvote 0
ActiveSheet.Next is not a good choice in this instance, use Worksheets("AWB") instead since it does not rely on the position of worksheets in the workbook.

Code:
Sub Print_AWB()

' Print_AWB Macro
' Prints AWB automatically.

' Keyboard Shortcut: Ctrl+Shift+B

    Dim I As Integer
    Dim R As Integer    'What is R for ?
    
    Worksheets("DATA").Select
    
    I = 2
    Do Until IsEmpty(Cells(I, 3))
        Cells(R + I, 3).Copy Destination:=Worksheets("AWB").Next.Range("R4")
        Worksheets("AWB").PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
        I = I + 1
    Loop
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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