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:[TABLE="width: 500"]
<TBODY>[TR]
[TD]7/1/11[/TD]
[TD]8/1/11[/TD]
[TD]9/1/11[/TD]
[TD]10/1/11[/TD]
[TD]11/1/11[/TD]
[TD]12/1/11[/TD]
[TD]1/1/12[/TD]
[TD]2/1/12[/TD]
[TD]3/1/12[/TD]
[TD]4/1/12[/TD]
[TD]5/1/12[/TD]
[TD]6/1/12[/TD]
[TD]7/1/12[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]120[/TD]
[TD]127[/TD]
[TD]129[/TD]
[TD]129[/TD]
[TD]135[/TD]
[TD]137[/TD]
[TD]141[/TD]
[TD]138[/TD]
[TD]131[/TD]
[TD]140[/TD]
[TD]129[/TD]
[TD]136[/TD]
[/TR]
[TR]
[TD]1y[/TD]
[TD]1y[/TD]
[TD]3n[/TD]
[TD]4y[/TD]
[TD]8n[/TD]
[TD]11n[/TD]
[TD]12y[/TD]
[TD]16y[/TD]
[TD]13n[/TD]
[TD]11n[/TD]
[TD]12y[/TD]
[TD]13n[/TD]
[TD]13n[/TD]
[/TR]
</TBODY>[/TABLE]
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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