Looping VBA Copy/Paste and Print to PDF (CODE)

Crypsis

New Member
Joined
May 8, 2018
Messages
5
Hi All,

Thanks in advance for your help.

I am trying to copy cells from a data set in rows A4:A150 into cell M5 on a different sheet. After each copy, i want the code to print to PDF and save this as is. When this is complete, i am trying to get the code to loop down to the next cell (so starting from A4 to A5 and so on).

This is the code i have, currently it will copy and paste the cell from A4 but it will not loop thereafter. Any suggestions?

Also, if you have any better ideas of writing this code then please let me know!

Code:
Sub PrintAll()

    ThisWorkbook.Sheets("Structured Note Raw Data").Range("A4").Copy
    ThisWorkbook.Sheets("Template").Range("M5").Select
    ThisWorkbook.Sheets("Template").Paste


Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet


'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"


strName = wsA.Range("M5").Value _


'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile


'export to PDF in current folder
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strPathFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & strPathFile


exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler


Dim cell As Range


'Loop through each cell in a cell range
  For Each cell In ThisWorkbook.Sheets("Structured Note Raw Data").Range("A4:A150")
    Debug.Print cell.Value
  Next cell
  


End Sub


Thanks very much.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about
Code:
Sub PrintAll()



Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim cell As Range

On Error GoTo errHandler


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

For Each cell In ThisWorkbook.Sheets("Structured Note Raw Data").Range("A4:A150")
    
    
   cell.Copy ThisWorkbook.Sheets("Template").Range("M5")
   strName = wsA.Range("M5").Value _


'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile


'export to PDF in current folder
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        fileName:=strPathFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & strPathFile
  Next cell


exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
 
Upvote 0
Fluff that is excellent!

Works like a dream, thank you very very much.

I'll look into it in a bit more detail so that I can learn about the changes you made.

Again, thanks so much and have a great weekend!

Best,
Crypsis
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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