Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Vb to set print area for dynamic pages

I have a workbook with four pages, each with the same number of columns, but
various numbers of rows. I have a macro to automatically set the print area
for those pages and it works just fine. Now I have to add a column to just
one of the pages in the workbook. The macro I have naturally excludes the
new last column. I suspect more changes are on the horizon i.e some pages
growing, others shrinking. I'd rather have Excel 'see' what columns are
present, rather than continually altering the code when I make changes. I
tried to use XLRight instead of naming the column "U", but that doesn't work.
I think I'm on the right track but probably not using the correct syntax.
Here is the code. Any one have any ideas?

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Activate
Dim LastRow As Long

With ActiveSheet
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
.PageSetup.PrintArea = "A1:U" & LastRow
End With
Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Vb to set print area for dynamic pages


Try this:

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Activate
Dim LastRow As Long
Dim LastCol As String

With ActiveSheet
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
End With

'Put in the next range selection the column where you have field names,
in this example is A1
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
LastCol = Selection.Address
LastCol = Mid(LastCol, InStr(LastCol, ":") + 2)
LastCol = Left(LastCol, InStr(LastCol, "$") - 1)

ActiveSheet.PageSetup.PrintArea = "A1:" & LastCol & LastRow
Next


Regards.


--
dbarelli
------------------------------------------------------------------------
dbarelli's Profile: http://www.excelforum.com/member.php...o&userid=31275
View this thread: http://www.excelforum.com/showthread...hreadid=509454

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Vb to set print area for dynamic pages

I'm getting a "Select Method of Range Class Failed" error. I put in the
range where I have field names as you suggested. I tried other ranges, but
still get that error.

"dbarelli" wrote:


Try this:

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Activate
Dim LastRow As Long
Dim LastCol As String

With ActiveSheet
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
End With

'Put in the next range selection the column where you have field names,
in this example is A1
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
LastCol = Selection.Address
LastCol = Mid(LastCol, InStr(LastCol, ":") + 2)
LastCol = Left(LastCol, InStr(LastCol, "$") - 1)

ActiveSheet.PageSetup.PrintArea = "A1:" & LastCol & LastRow
Next


Regards.


--
dbarelli
------------------------------------------------------------------------
dbarelli's Profile: http://www.excelforum.com/member.php...o&userid=31275
View this thread: http://www.excelforum.com/showthread...hreadid=509454


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Vb to set print area for dynamic pages


Be shure to run this code inside a Module. After that if still not
working, really dont know. It works for me.

Regards


--
dbarelli
------------------------------------------------------------------------
dbarelli's Profile: http://www.excelforum.com/member.php...o&userid=31275
View this thread: http://www.excelforum.com/showthread...hreadid=509454

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Vb to set print area for dynamic pages

Got it to work, I was being a pinhead. Thanks for your help.

"dbarelli" wrote:


Be shure to run this code inside a Module. After that if still not
working, really dont know. It works for me.

Regards


--
dbarelli
------------------------------------------------------------------------
dbarelli's Profile: http://www.excelforum.com/member.php...o&userid=31275
View this thread: http://www.excelforum.com/showthread...hreadid=509454




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove print area from multiples pages Linda Excel Discussion (Misc queries) 1 April 8th 10 06:21 PM
Print Area ranges print on separate pages? Lyndon Excel Discussion (Misc queries) 1 December 29th 06 05:22 PM
Set print area on several pages at once in Excell Workbook? willow Excel Discussion (Misc queries) 2 August 23rd 05 01:31 AM
Print area will only go up to 101 pages Martc Excel Discussion (Misc queries) 1 June 23rd 05 12:50 AM
print pages when text area changes in size dizzy Excel Programming 0 January 25th 04 01:28 AM


All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"