+ Reply to Thread
Results 1 to 14 of 14

Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

  1. #1
    Registered User
    Join Date
    02-06-2013
    Location
    Mexico
    MS-Off Ver
    MS Office 2016
    Posts
    99

    Exclamation Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    A form has been designed to dynamically generate checkboxes for all active, visible sheets in the workbook, enabling users to select specific sheets for PDF printing. However, when more than 7 sheets are selected, the process fails with a "Subscript out of Range" (Error 9).

    Various attempts have been made to resolve this issue, but the solution for handling more than 7 sheets remains elusive. Assistance is needed to identify the necessary changes in the code to ensure it functions correctly with a larger selection of sheets.

    The subroutine responsible for PDF generation can be found under "cmdOk_Click." The file with the module is attached for reference. Any guidance would be appreciated.

    Thanks!

    Please Login or Register  to view this content.
    PDF_Book1.xlsm
    Attached Files Attached Files
    Last edited by pacosalasv; 09-27-2024 at 09:14 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    When you have an error like this, disable
    On Error
    because it hides the error. When I remove it, it takes me to this line of code
    Please Login or Register  to view this content.
    When I dump the contents of the array, the first element is
    SELECT ALL
    which is not the name of an existing sheet. That causes the subscript error.

    You are collecting sheet names based on the name of the checkboxes.
    Please Login or Register  to view this content.
    Your checkbox for SELECT ALL is named ChkAll but you are testing for chkAll. Text comparisons are case-sensitive.

    Changing this line of code will resolve the problem.
    Please Login or Register  to view this content.
    By the way the best practice for design is to define your UserForm as a user interface layer, and then put your business logic in separate modules. This design puts all the logic in the UserForm.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-06-2013
    Location
    Mexico
    MS-Off Ver
    MS Office 2016
    Posts
    99

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    6StringJazzer,

    Thank you for your suggestion regarding the "chkAll" issue. Unfortunately, the problem persists.

    When selecting seven or fewer sheets, the process works seamlessly, and the PDF is generated as expected. However, when selecting more than seven sheets, only the last seven pages are printed by the macro, instead of all selected.

    Do you have any further recommendations on how to resolve this?

    Additionally, I will be sure to implement your suggested best practices advice moving forward.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    That's a different problem than your initial report of a runtime error. I'll look into it.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    You are batching sheets in groups of 10, not 7. I am able to print 10 sheets with no problem. The problem occurs with more than 10.

    The problem is that you are using the same file name every time, so you are overwriting the file. The last batch is the last time it's written.
    Please Login or Register  to view this content.
    Also, you have this comment:
    Please Login or Register  to view this content.
    but this is not correct. I have not found any limit. I think your whole approach can be greatly simplified to print all selected sheets at once.

    Try running this demo code, which shows that you can select all sheets in the file:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    02-06-2013
    Location
    Mexico
    MS-Off Ver
    MS Office 2016
    Posts
    99

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Thank you again for your response, but unfortunately, the issue persists and the functionality still isn’t behaving as expected. I do agree with your suggestion of printing all selected sheets at once; I had previously attempted batching but encountered the same results.

    Here’s a summary of my findings based on the tests I conducted, all using your provided stest subroutine:

    Test 1: When the "Select All" checkbox is marked, including all the sub-checkboxes, I receive the error: "An error occurred during PDF Export: Subscript out of range."

    Test 2: If "Select All" is unchecked, and I manually select each individual checkbox, the same error appears: "An error occurred during PDF Export: Subscript out of range."

    Test 3: When "Select All" is unchecked, and I choose 1 to 10 sheets, the export process works correctly without any issues.

    Test 4: If "Select All" is unchecked, and I select 11, it only prints sheet 11.

    Test 5: When "Select All" is unchecked and more than 10 sheets are selected, only the sheets corresponding to the second digit of the selected sheet number are printed. For example, if sheet 12 is selected, only 2 sheets gets printed; if sheet 15 is selected, only 5 sheets gets printed.

    Given these anomalies, I'm quite perplexed and unsure how to address this. Could you replicate these steps with the file I uploaded to see if you encounter the same behavior? Any insights or suggestions you can provide would be greatly appreciated.

    Thank you again for your ongoing support!

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Quote Originally Posted by pacosalasv View Post
    Test 4: If "Select All" is unchecked, and I select 11, it only prints sheet 11.
    I explained this one already. The code prints all batches to the same file. Did you modify the code to print each batch to a different file?

    I would totally rewrite your code but I don't have time to do that today. I can revisit tomorrow.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    If you want to do away with checkboxes, put this code in a module and run it.

    Please Login or Register  to view this content.
    The inherent weakness of the liberal society: a too rosy view of humanity.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    I would probably do this with a multiselect listbox instead of dynamically creating checkboxes or other controls. Will revisit when time permits, but I think it needs an overhaul.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Which post are you referring to Jeff?
    The code in Post #8 creates a UserForm with a Label, Multiselect Listbox and 2 (two) command buttons, one each for selecting some visible sheets and one to save all visible sheets to pdf.
    The Listbox is populated with visible sheets only. The first sheet is not included because I have it as a data sheet
    It is just another possibility. Normally one would have have a UserForm with all the required controls instead of generating it with code.
    It might be a problematic code for someone with less than 100 posts, if that indeed shows the experience, but like I said, something to play with.
    Help is only a question away.

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Quote Originally Posted by jolivanes View Post
    Normally one would have have a UserForm with all the required controls instead of generating it with code.
    That would be my personal preference. I am not a fan of dynamically creating controls when everything you need can be done statically, and I'm also not a fan of writing code to write code due to the added layer of difficulty in debugging.

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    OK. You convinced me.

    See attached
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    02-06-2013
    Location
    Mexico
    MS-Off Ver
    MS Office 2016
    Posts
    99

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Quote Originally Posted by jolivanes View Post
    OK. You convinced me.

    See attached
    6StringJazzer / Jolivanes,

    Your solution works flawlessly! I truly appreciate your different approach and the effort you put into redesigning the form and code.

    It's always a privilege to learn from experts like you. Wishing you a great weekend!

    I'll mark the post as solved—your support has been outstanding.

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Error "Subscript out of Range" When Printing More than 7 Sheets to PDF

    Thank you for the update and the kind words.
    Good luck

+ 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. [SOLVED] Why my code is producing Error "SUBSCRIPT OUT OF RANGE" While using variant arrays?
    By Asad Mir in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2020, 10:22 AM
  2. "Run-time error '9': Subscript out of range" when referencing between workbooks
    By roberts23 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-08-2014, 10:33 AM
  3. Breaking out Comma Delimited Fields - VBA Error - "Subscript out of Range"
    By sev979 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-13-2014, 09:55 AM
  4. [SOLVED] trying to understand why a code is not working "error 9 subscript out off range"
    By cdafonseca in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-09-2013, 12:33 PM
  5. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM
  6. [SOLVED] "Subscript out of range" error for: Workbooks("Test1.xls").Save
    By Just12341234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2005, 11:05 AM
  7. FileCopy Command Giving "Subscript Out of Range" Error Message
    By Jim Hagan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2005, 02:05 PM

Tags for this Thread

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