macro with print area

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
444
Office Version
  1. 2019
hello, i would like to figure out the code of print area
i'm not sure how can i start with B22 and end with XXX in my sheet?
should i use this code with ending: row = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row?

PS: XXX is not same as always.

thanks in advance
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is XXX the last row? If so maybe

Code:
    LastRow = ActiveSheet.Columns("B:B").Find(What:="*" _
    , After:=[B22], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ActiveSheet.PageSetup.PrintArea = "$B$22:$B$" & LastRow
 
Last edited:
Upvote 0
Is XXX the last row? If so maybe

Code:
    LastRow = ActiveSheet.Columns("B:B").Find(What:="*" _
    , After:=[B22], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ActiveSheet.PageSetup.PrintArea = "$B$22:$B$" & LastRow

yes, XXX = last row
sometime might: b22:T50, b22:t55, b22:r30.......
 
Upvote 0
yes.
actually, it's a daily roster i have to print out everyday. however it's not same as always, so i would like to set a code to run my roster and print area.
 
Upvote 0
Try untested...

Code:
 LastRow = Cells.Find(What:="XXX" _
    , After:=[B22], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ActiveSheet.PageSetup.PrintArea = Range("B22:B" & LastRow).Address

Change the single B to your last column
 
Last edited:
Upvote 0
The last column isn't known.

If all of your cells have data in them then the following will work. If there are blanks on row 22 or in column B then it won't work.

Code:
    Range("B22").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.PageSetup.PrintArea = Selection.Address
 
Upvote 0
Now I am by a computer, if the last column isn't known then the code below will deal with the possibility of empty cells and doesn't use selects.


Code:
Sub XXXX()
    Dim LastRow As Long, LastCol As Long

    LastRow = Cells.Find(What:="*" _
                               , After:=Range("B" & Rows.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    LastCol = Rows("22:" & LastRow).Find("*", , xlValues, xlPart, xlByColumns, xlPrevious, False).Column

    ActiveSheet.PageSetup.PrintArea = Range(Cells(22, "B"), Cells(LastRow, LastCol)).Address
End Sub
 
Last edited:
Upvote 0
Or a little shorter and allowing for B not being the longest column (although in this case it is by what the OP has stated).

Code:
Sub XXXX()
    Dim LastRow As Long, LastCol As Long

    LastRow = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    
    LastCol = Rows("22:" & LastRow).Find("*", , , , xlByColumns, xlPrevious).Column

    ActiveSheet.PageSetup.PrintArea = Range(Cells(22, "B"), Cells(LastRow, LastCol)).Address
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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