+ Reply to Thread
Results 1 to 13 of 13

VBA problem with Print Range code run-time error 1004 text not valid reference

  1. #1
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    VBA problem with Print Range code run-time error 1004 text not valid reference

    The code below copies my workbook which has my data and code and, saves as "Air Hours with Month from data worksheet.
    I then reopen the saved file and am trying to format the print area. My code keeps stalling at .PrintArea = "PRange"
    I'm getting a run time error 1004 - Text entered is not a valid reference. I'm not sure what is wrong.

    Please Login or Register  to view this content.
    Could someone please help me out?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    PRange is a variable in your code, it's not a named range.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Try this...

    .PrintArea = PRange.Address
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Thanks AlphaFrog, that works.

    There must be something wrong with my print area though. The report I'm setting the print area for is a pivot table. It's only setting column A of the pivot table as the print area not the entire pivot table. Would you be able to help with that?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Quote Originally Posted by Jeep56 View Post
    Thanks AlphaFrog, that works.

    There must be something wrong with my print area though. The report I'm setting the print area for is a pivot table. It's only setting column A of the pivot table as the print area not the entire pivot table. Would you be able to help with that?
    This is not tested.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    You can get the range of a pivot table using the property TableRange1(TableRange2 to include page fields).

  7. #7
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    I'm getting an error 91 object variable or with block not set at line FinalRow = wks.Cells.Find("*", , , , 1, 2).Row
    The report then appears and, it seems to have the print areas set correctly. Just need to get rid of the Error 91

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    You'll get that error if any worksheets are blank.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Hi Norie

    How would I code this? I keep making pivot tables as they're so useful but, I have great difficulty trying to refer back to them.

    Dim Pt as pivottable
    .PrintArea = Pt.tableRange2.address

  11. #11
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Thanks for your help. I was getting the error code 91 because I had an empty worksheet. Your initial code worked AlphaFrog bit I will add in the "IF" statement as an extra percaution.

    10 months ago, I'd never heard of VBA...I have learned so much from this forum

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    You would need to set Pt to the pivot table.

    Also, you probably don't want to use the exact address of the TableRange2 for the print area.

    For example, if there was only one pivot table on Sheet4 this code would set the print area from A1 to the bottom right corner of the pivot table.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-10-2013
    Location
    Manitoba, Canada
    MS-Off Ver
    2010
    Posts
    28

    Re: VBA problem with Print Range code run-time error 1004 text not valid reference

    Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. The Sort reference is not valid - run time error
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-23-2014, 09:14 AM
  2. Need help fixing macros due to Run-Time error 1004 Sort Reference is Not Valid
    By travisg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2013, 12:13 AM
  3. runtime error 1004 data base or list range not valid
    By jay11 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2010, 03:08 PM
  4. Run time error 1004 Reference is not valid on GoalSeek
    By Ted Heaslip in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2009, 09:28 AM
  5. pivot table refresh - error 1004 - reference is not valid
    By jrussell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-14-2008, 05:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1