VBA code - Set Print Area based on last row data

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm looking to Set Print Area from Row 1 to the last Row where there is data (under column B). However, there is a twist. (And if the twist isn't easily done, just finding the last row where data is entered under column B will be fine)

If the last entered data is the word "Display", the Print Area should be from Row 1 and then 3 rows up from the word 'Display' (Row 10 in the example below)
1707926367744.png


If the last entered data is NOT the word "Display", the Print Area should be from Row 1 to that last entered data row (Row 15 in the example below)
1707926521197.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this:
VBA Code:
Sub SetPrintRange()

    Dim lr As Long
    Dim rng As Range
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Set print range
    If Cells(lr, "A").Value = "Display" Then
        Set rng = Range("A1:A" & lr - 3)
    Else
        Set rng = Range("A1:A" & lr)
    End If
    
'   Set print area
    ActiveSheet.PageSetup.PrintArea = rng.Address
    
End Sub
 
Upvote 0
Solution
You are welcome!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,223,967
Messages
6,175,667
Members
452,666
Latest member
AllexDee

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