VBA Loop: Enter Value in a Cell then Save a Subset of Sheets as pdf

itord1973

New Member
Joined
Sep 11, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi guys. This is my first post.

I created an eternal calendar in Excel. I need to come up with a loop that would feed one of the cells with a specific starting year, let Excel do its thing, and then print the results as pdf file for each year. Leap years and common years would print from different sheets. For example, a common year would print 'Calendar_0', 'Calendar_1', and 'Calendar_3' but a leap year would print 'Calendar_0', 'Calendar_2', and 'Calendar_4'. The range of years is from years 1900 through 5100. I tried recording a macro and modifying it but I got nowhere.

I would really appreciate your help.

Ivan
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Which is the exact cell where the starting year should be put? In the macro below, this cell is A2 on the sheet named "Year". The macro then loops though the years 1900 to 1908, saving the specific sheets as the year PDF (1900.pdf, 1901.pdf, etc) in the specified folder.

VBA Code:
Public Sub Create_Calendar_PDFs()

    Dim saveInFolder As String
    Dim yearCell As Range
    Dim y As Integer
    
    saveInFolder = "C:\Temp\"  'folder path where PDFs are created
    
    Set yearCell = Worksheets("Year").Range("A2")
    
    For y = 1900 To 1908
        yearCell.Value = y
        If IsLeapYear(y) Then
            Worksheets(Array("Calendar_0", "Calendar_2", "Calendar_4")).Select
        Else
            Worksheets(Array("Calendar_0", "Calendar_1", "Calendar_3")).Select
        End If
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & y & ".pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
    
    yearCell.Worksheet.Select
    
End Sub


Private Function IsLeapYear(y As Integer) As Boolean
    IsLeapYear = Month(DateSerial(y, 2, 29)) = 2
End Function
 
Upvote 1
Solution
John_W, you are an absolute genius! Thank you! It's working perfectly!

Regards,
Ivan
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,105
Members
449,096
Latest member
provoking

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