Copy, Paste, Print to PDF and Loop (With Code)!

Status
Not open for further replies.

Crypsis

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

Thanks for taking the time to look at my post!

I am attempting to copy and paste data one by one from the range A4:A150 to cell M5. After each copy and paste, i would like the macro to save to PDF and then loop to the next cell from the range.

At the moment my code works for the first cell only (A4) and is not looping for the rest of the data.

Here is my code:

Code:
Sub PrintAll()[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    ThisWorkbook.Sheets("Structured Note Raw Data").Range("A4").Copy[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    ThisWorkbook.Sheets("Template").Range("M5").Select[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    ThisWorkbook.Sheets("Template").Paste[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim wsA As Worksheet[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim wbA As Workbook[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim strName As String[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim strPath As String[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim strFile As String[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim strPathFile As String[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim myFile As Variant[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]On Error GoTo errHandler[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Set wbA = ActiveWorkbook[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Set wsA = ActiveSheet[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]'get active workbook folder, if saved[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]strPath = wbA.Path[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]If strPath = "" Then[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]  strPath = Application.DefaultFilePath[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]End If[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]strPath = strPath & "\"[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]strName = wsA.Range("M5").Value _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]'create default name for savng file[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]strFile = strName & ".pdf"[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]strPathFile = strPath & strFile[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]'export to PDF in current folder[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    wsA.ExportAsFixedFormat _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        Type:=xlTypePDF, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        Filename:=strPathFile, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        Quality:=xlQualityStandard, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        IncludeDocProperties:=True, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        IgnorePrintAreas:=False, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        OpenAfterPublish:=False[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    'confirmation message with file info[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    MsgBox "PDF file has been created: " _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]      & vbCrLf _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]      & strPathFile[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]exitHandler:[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    Exit Sub[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]errHandler:[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    MsgBox "Could not create PDF file"[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    Resume exitHandler[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Dim cell As Range[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]'Loop through each cell in a cell range[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]  For Each cell In ThisWorkbook.Sheets("Structured Note Raw Data").Range("A4:A150")[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    Debug.Print cell.Value[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]  Next cell[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]  [/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]End Sub[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]

If you have any better ideas or ways or writing it then please let me know!

Thanks,
Crypsis
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Status
Not open for further replies.

Forum statistics

Threads
1,224,828
Messages
6,181,204
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