VBA to find and reference a cell with date closest to today but not greater - set print area from that

HRIS

Board Regular
Joined
Dec 29, 2010
Messages
116
I have a spreadsheet with dates across row 1 and information related to the dates in the columns below. The cells are formatted to show month and year (so it shows July-12 for this month with the actual date entered being 7/1/12).

I want to auto set the print area, upon sheet activation, so that it sets the print area as the columns for the 12 previous months before the current month's column. So I was thinking I would set the print area range by starting with the cell that has a date < Date & >= Date - 28 then offsetting -12 columns and -86 rows. However, I am struggling with the code to find and reference the cell with the previous month's date as mentioned.

Any thoughts/experience you can share is appreciated!

Example, this month I would want to set the print area as the months & their columns that are in blue below:
7/1/118/1/119/1/1110/1/1111/1/1112/1/111/1/122/1/123/1/124/1/125/1/126/1/127/1/12
123120127129129135137141138131140129136
1y1y3n4y8n11n12y16y13n11n12y13n13n

<TBODY>
</TBODY>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
try this

Code:
Sub SetPrint()
Dim i, j, LC, LR, As Long
Dim PDate As Date
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
LC = ActiveSheet.Cells(1, "IV").End(xlToLeft).Column
PDate = Date - 365
For i = 1 To LC
    If WorksheetFunction.Text(Cells(1, i), "mm-yyyy") = WorksheetFunction.Text(PDate, "mm-yyyy") Then
        StartA = Cells(1, i).Address
        StartE = Cells(1 + 86, i + 11).Address
        ActiveSheet.PageSetup.PrintArea = Range(StartA, Range(StartE)).Address
        GoTo 0
    End If
Next i
End Sub
 
Upvote 0
Thank you for taking a look at this!

On the line "GoTo 0" I am getting "Compile error: Label not defined" ?
 
Upvote 0

Forum statistics

Threads
1,215,622
Messages
6,125,886
Members
449,269
Latest member
GBCOACW

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