Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 07-02-2009, 08:45 PM
froodo froodo is offline
Registered User
 
Join Date: 02 Jul 2009
Location: Australia
MS Office Version:Excel 2003
Posts: 3
froodo is becoming part of the community
PageSetup properties using variables

Please Register to Remove these Ads

Hi

I am getting error:
Run-time error '1004': Unable to set the Orientation property of the PageSetup class

I am using the following code:
Code:
Dim StrOrientation As String
Application.Goto Reference:="Orientation"
StrOrientation = Activecell.Value 'where reference is either xlPortrait of xlLandscape


    With ActiveSheet.PageSetup
        
         .Orientation = StrOrientation 

    End With
In the .Orientation = StrOrientation section I have replaced the variable with "xlPortrait" and it works so I know it isn't a printer issue. I have entered the variable reference in incorrectly but I cannot work out where or how.


Thanks in advance

Last edited by froodo; 07-08-2009 at 09:25 PM. Reason: code tag
Reply With Quote
  #2  
Old 07-02-2009, 09:22 PM
Trebor76 Trebor76 is offline
Valued Forum Contributor
 
Join Date: 10 Dec 2006
Location: Sydney, Australia
MS Office Version:2002 - 2007 (work), 2003 (home)
Posts: 731
Trebor76 has an addiction to Excel
Re: PageSetup properties using variables

Hi froodo,

Welcome to the forum.

I may have a solution, but before I can suggest it you'll need to PM a moderator to have your code wrapped as your thread currently breaks Rule 3 of the forum (see here for more info).

Once this is done I'll gladly post my suggested solution.

Regards,

Robert
__________________
____________________________________________
Please ensure you mark your thread as Solved once it is. Click here to see how
If this post helps, please don't forget to add to our reputation by clicking the blue scale icon in the top right-hand corner of my post
Reply With Quote
  #3  
Old 07-02-2009, 10:11 PM
Trebor76 Trebor76 is offline
Valued Forum Contributor
 
Join Date: 10 Dec 2006
Location: Sydney, Australia
MS Office Version:2002 - 2007 (work), 2003 (home)
Posts: 731
Trebor76 has an addiction to Excel
Re: PageSetup properties using variables

Hi froodo,

Thanks for that.

As you've created a named range you don't have to assign its value to variable, i.e.

Code:
Sub Macro1()
           
    With ActiveSheet.PageSetup
    
        .Orientation = Range("Orientation").Value
    
    End With

End Sub
will work fine. If you really want to assign the value of the "Orientation" named range to a variable (which seems to me to be doubling handling), try this:

Code:
Sub Macro2()

    Dim strOrientation As String
    
    strOrientation = Range("Orientation").Value
        
    With ActiveSheet.PageSetup
    
        .Orientation = strOrientation
    
    End With

End Sub
HTH

Robert
__________________
____________________________________________
Please ensure you mark your thread as Solved once it is. Click here to see how
If this post helps, please don't forget to add to our reputation by clicking the blue scale icon in the top right-hand corner of my post
Reply With Quote
  #4  
Old 07-02-2009, 10:26 PM
froodo froodo is offline
Registered User
 
Join Date: 02 Jul 2009
Location: Australia
MS Office Version:Excel 2003
Posts: 3
froodo is becoming part of the community
Re: PageSetup properties using variables

Thanks for your reply and help with forum etiquette, I still am receiving the same error when I use either of your methods. Any other ideas what may be wrong?


Thanks
Reply With Quote
  #5  
Old 07-02-2009, 11:31 PM
Trebor76 Trebor76 is offline
Valued Forum Contributor
 
Join Date: 10 Dec 2006
Location: Sydney, Australia
MS Office Version:2002 - 2007 (work), 2003 (home)
Posts: 731
Trebor76 has an addiction to Excel
Re: PageSetup properties using variables

Hi froodo,

The orientation options (i.e. Portrait or Landscape) must be returned as integers (1 or 2 respectively).

Have a look at the following which checks the value in the "Orientation" named range to assign one of the two values to the "intPrintOrientation" accordingly. I've also included some notes to hopefully explain things:

Code:
Sub Macro3()

    'Declare variable
    Dim intPrintOrientation As Integer
    
    'If any part of the named range "Orientation" has _
    'Portrait' in it then...
    If InStr(Range("Orientation").Value, "Portrait") > 0 Then
        '...assign 1 (integer value for portrait) to the _
        'intPrintOrientation' variable.
        intPrintOrientation = 1
    'Else...
    Else
        '...assign 2 (integer value for landscape) to the _
        'intPrintOrientation' variable (this will therefore _
        be the default).
        intPrintOrientation = 2
    End If

    With ActiveSheet
        .PageSetup.Orientation = intPrintOrientation
        .PrintOut Copies:=1
    End With
        
End Sub
HTH

Robert
__________________
____________________________________________
Please ensure you mark your thread as Solved once it is. Click here to see how
If this post helps, please don't forget to add to our reputation by clicking the blue scale icon in the top right-hand corner of my post
Reply With Quote
  #6  
Old 07-08-2009, 09:25 PM
froodo froodo is offline
Registered User
 
Join Date: 02 Jul 2009
Location: Australia
MS Office Version:Excel 2003
Posts: 3
froodo is becoming part of the community
Smile Re: PageSetup properties using variables

Thanks, that did the trick. I was thinking that the xlLandscape and xlPortrait would of have to of been used.

Cheers
Reply With Quote


Reply

Bookmarks

Tags
1004 , pagesetup property , variable


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Forum Jump