Page Orientation Macro

agc3278

New Member
Joined
Nov 14, 2007
Messages
15
I have a macro that hides and un-hides rows, which was graciously created with the help of this forum (see below). I am looking to adjust it (if possible) so that depending on the number of rows visible, the page orientation will change from portrait to landscape, and vice versa.

If it is not possible, I would love some assistance on writing a separate macro to do what I have described. Any help would be greatly appreciated.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim aNames, Val
  Dim i As Long, ShowHideRows As Long, ShowRows As Long
  
  Const sNames As String = "Sheet 1"
  Const RowCell As String = "D1"
  Const FirstVisRow As Long = 4
  Const MaxHideRow As Long = 41
  
  If Target.Address(0, 0) = RowCell Then
    aNames = Split(sNames, ", ")
    ShowHideRows = MaxHideRow - FirstVisRow + 1
    Val = Target.Value
    ShowRows = IIf(IsEmpty(Val), ShowHideRows, Val)
    Application.ScreenUpdating = False
    For i = 0 To UBound(aNames)
      With Sheets(aNames(i)).Rows(FirstVisRow)
        .Resize(ShowHideRows).Hidden = True
        If ShowRows > 0 Then .Resize(ShowRows).Hidden = False
      End With
    Next i
    Application.ScreenUpdating = True
  End If
End Sub
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
How about adding something like this to your code? Add it after Next i...granted you will need to determine what the value of rowamount is going to be
Code:
Application.PrintCommunication = False
    With ActiveSheet.PageSetup
if rowamount => then
        .Orientation = xlLandscape
else
.Orientation = xlportrait
endif
    End With
Application.PrintCommunication = True
 

agc3278

New Member
Joined
Nov 14, 2007
Messages
15
Thanks so much! I will give it a try when I'm back at work. Will the code start on the next line where the Application.Screenupdating part would be, and then bookend it with the screen updating part?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,257
Messages
5,769,075
Members
425,515
Latest member
baltusf

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
Top