Is there a way to find the last column in a worksheet?

zombiemaster

Board Regular
Joined
Oct 27, 2009
Messages
241
I know about endRow$ = ActiveCell.Row but is there a similar way to find the last column the same way? I tried versions of EndColumn$ = ActiveCell.Column but it didn't seem to work...

What I'm trying to do is set the print area for the entire range of data on a worksheet but the number of rows and columns can change from day to day. When I run the report each day, I have to go in and manually drag the print area over to encompass all the columns.

Thanks for any help!

~ZM~
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Perhaps

Code:
LastCol = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
 
Upvote 0
Thanks VoG - I'm not sure I understand what it's doing so I need to ask for clarification (as usual!)...here is the code I used today since I knew my last column was going to be "AA". Starting in cell A4, how would I incorporate your code into this so my print range would find the end row and the end column automatically?

Code:
ActiveSheet.PageSetup.PrintArea = ("$A$4:$AA" + endRow$)

~ZM~
 
Upvote 0
Try

Code:
Sub test()
Dim LastRow As Long, LastCol As Long
With ActiveSheet
    LastRow = .Columns(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    LastCol = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    .PageSetup.PrintArea = .Range(.Cells(4, 1), .Cells(LastRow, LastCol)).Address
End With
End Sub
 
Upvote 0
That worked perfectly, VoG - I was able to incorporate that code in a few places I needed it throughout the entire script for multiple sheets.

Thank you very much - you ROCK!

:cool:
~ZM~
 
Upvote 0
What I always use:

lastrow = record.UsedRange.Rows(record.UsedRange.Rows.Count).row

Where record is the sheet's name property
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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