+ Reply to Thread
Results 1 to 8 of 8

Error in Macro to print a Range

  1. #1
    Forum Contributor
    Join Date
    07-21-2009
    Location
    Kuwait
    MS-Off Ver
    Excel 2019
    Posts
    292

    Angry Error in Macro to print a Range

    Sub test()
    Dim LastRowToPrint As Long
    Dim PrintArea As Range
    Dim FirstCellToPrint As String
    Dim LastColumnToPrint As String

    FirstCellToPrint = "A1"
    LastColumnToPrint = "E"

    'Find the last row of data:
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Set PrintArea = Range(FirstCellToPrint & ":" & LastColumnToPrint & LastRowToPrint)

    End Sub
    I m getting a probelm in above code and need little immediately, when i run it a message appear that "Method "Range" of object _Global Failed..

    Can some one correct it..Sheet is attached i want to print 3 ranges as in sheet from 3 different macros but range always decreasing and increasing.

    Thanks in advance

    Book1.xls
    Last edited by tariqnaz2005; 11-11-2009 at 08:54 AM.

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Error in Macro to print a Range

    You have used "quote" instead of code tags, but the intent is there I suppose... (edit, I have too, apparently bold doesn't render in code tags)

    see highlighting, what is this variable?

    Sub test()
    Dim LastRowToPrint As Long
    Dim PrintArea As Range
    Dim FirstCellToPrint As String
    Dim LastColumnToPrint As String

    FirstCellToPrint = "A1"
    LastColumnToPrint = "E"

    'Find the last row of data:
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Set PrintArea = Range(FirstCellToPrint & ":" & LastColumnToPrint & LastRowToPrint)

    End Sub
    Clearly:
    LastRowtoprint = Cells(Rows.Count, "A").End(xlUp).Row
    is necessary.

    This kind of error can be avoided by applying option explicit at the top of every code module, You can even set a global setting to apply it automatically: tools -> options -> require variable declaration.

    HTH
    Last edited by Cheeky Charlie; 11-11-2009 at 08:52 AM. Reason: code tags to quote tags

  3. #3
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Error in Macro to print a Range

    !!!!!

    Please DO NOT cross-post without references

    http://www.excelforum.com/excel-gene...-in-macro.html

  4. #4
    Forum Contributor
    Join Date
    07-21-2009
    Location
    Kuwait
    MS-Off Ver
    Excel 2019
    Posts
    292

    Re: Error in Macro to print a Range

    I m sorry if i bothered you..

    But i want to solve it..and your reply..i did,t get it..

    Can you explain me again, thanks

  5. #5
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Error in Macro to print a Range

    Replace:
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-21-2009
    Location
    Kuwait
    MS-Off Ver
    Excel 2019
    Posts
    292

    Re: Error in Macro to print a Range

    You can even set a global setting to apply it automatically: tools -> options -> require variable declaration.
    I did as you told but stil same error is coming

  7. #7
    Forum Contributor
    Join Date
    07-21-2009
    Location
    Kuwait
    MS-Off Ver
    Excel 2019
    Posts
    292

    Angry Re: Error in Macro to print a Range

    Thanks for quick response..

    Now it is corrected..but when i click button to print..it does,t work..can you check my attached sheet and correct it..

    I dont know this macro wil print directly or I have to do some thing else?

    Thanks

    Book1.xls

  8. #8
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Error in Macro to print a Range

    The macro adjusts the print range, as per your request in your original thread.
    add the line:
    Please Login or Register  to view this content.
    before the end sub to make it print - this will use settings as defined last time you printed.

    For any other questions start a new thread and link back to this.

    CC

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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