VBA Loop - Data sheet to pdf file

rowbro

Board Regular
Joined
Dec 16, 2010
Messages
50
Evening. I have a excel table (auction data sheet ) with various rows of data, and I need to put each row into another sheet (Output sheet) and then save these as a PDF with the Date, Manager and Bond, Client fields as the filename.

I do this manually by changing the "Lookup Current" number in the input sheet, and there are vlookups for the respective rows of data in the Output sheet.

However, I would like to automate this process so that I can loop through the lookup range in the input sheet, saving each one as a PDF as it loops through.

Can someone assist with some VBA for this? My effort falls far short.

VBA Code:
Sub TestBN()
'
' TestBN Macro
'

'
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("C5").Select
    Sheets("BrokersNote").Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
        "/Note_Automation.pdf", Quality _
        :=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    Sheets("Input").Select
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("C8").Select
    Sheets("BrokersNote").Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
        "/Note_Automation2.pdf", Quality _
        :=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    Sheets("Input").Select
    Range("A1").Select
End Sub
 

Attachments

  • Output sheet.png
    Output sheet.png
    71.9 KB · Views: 8
  • Input sheet.png
    Input sheet.png
    53.3 KB · Views: 8
  • Auction data.png
    Auction data.png
    190.5 KB · Views: 8

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this macro, although your Output sheet doesn't show the Client field in its own cell, so I've decided to use cell E21 so that the full PDF file name can be constructed.

VBA Code:
Public Sub Create_PDFs()

    Dim inputSheet As Worksheet, outputSheet As Worksheet
    Dim folder As String, PDFfile As String
    Dim i As Long
    
    With ActiveWorkbook
        Set outputSheet = .Worksheets("Output")
        Set inputSheet = .Worksheets("Input")
        folder = .Path & "\"
    End With
    
    With inputSheet
        For i = .Range("C5").Value To .Range("C6").Value
            .Range("C4").Value = i
            PDFfile = Format(outputSheet.Range("B10").Value, "YYYY-MM-DD") & " " & outputSheet.Range("B12").Value & " " & outputSheet.Range("E22").Value & " " & outputSheet.Range("E21").Value & ".pdf"
            outputSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=folder & PDFfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Next
    End With
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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