Select range betwen two dates, set print range and print to pdf

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
506
Office Version
  1. 365
Platform
  1. Windows
Hi folks
Struggling with some VBA.... as usual :(

I have a new sheet I'm working on. It has 8 columns of data A:H and headings in Row 1. Column A contains a date (d/m/yy) for data to be entered, there could well be multiple instances of the same date, so not a unique value. I have a userform to very simply (He says....) select a start date in textbox1 and an end date in textbox2 and a command button (CommandButton1) to action, which would then hopefully select a range between the 1st instance of start date and last instance of end date, set that as print range, including the headers in Row1 and print to pdf.
Is that feasible, or am I being daft, as usual? Not something I've tried before and not a clue where to start.
Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Put this code in a standard module:
VBA Code:
Public Sub Save_Dates_Range_As_PDF(startDate As Date, endDate As Date)

    With ThisWorkbook.ActiveSheet
        
        'Filter between start and end dates in column A
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(startDate), Operator:=xlAnd, Criteria2:="<=" & CLng(endDate)
    
        'Save filtered data as PDF file in same folder as this workbook
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\Data " & Format(startDate, "dd-mm-yyyy") & " to " & Format(endDate, "dd-mm-yyyy") & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
                    
        'Turn off autofilter
        .AutoFilterMode = False
    
    End With
    
End Sub
And call it from your Userform module's CommandButton1 Click event handler like this:
VBA Code:
Private Sub CommandButton1_Click()
    Save_Dates_Range_As_PDF TextBox1.Value, TextBox2.Value
End Sub
 
Upvote 0
Solution
I was just having a go, and thought I was getting somewhere, but your solution works perfectly.

I changed the open after publish to true, so it opens the pdf. If that was left as false.... where does it save it?

Thank you so much for your time and help..... us numpties would be lost without you guys.
 
Upvote 0
"If that was left as false.... where does it save it?"

Please ignore.... I managed to spot the obvious.....
Thanks again... works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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