+ Reply to Thread
Results 1 to 10 of 10

VBA Code Troubleshooting

  1. #1
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Lightbulb VBA Code Troubleshooting

    Hi Folks,

    I got some help from @ByteMarks on some code that would hide rows in various places in my spreadsheet depending on the value of a reference cell. My spreadsheet uses a main "Input" page to add information about a shipment of goods, and then a bunch of shipping documents are created (in other tabs) based on that information. The pages are then converted to PDF and sent to me via email. The code I'm trying to fix (that I obtained from this forum) hides rows in the other tabs where the shipping documents are created, with the express purpose of shortening the size of the document; ex: If I'm shipping 14x goods, there are 14 rows of data in these tabs; if I'm only shipping 2x goods, all rows but 2 are hidden to shorten the size of the PDF when it's printed.

    Here is the code, and where in the code the error is happening when I try to run it in my spreadsheet:

    Please Login or Register  to view this content.
    There is also the following private sub function for the "Input" tab where I have my reference cell:

    Please Login or Register  to view this content.


    For reference, the reference cell I use is I21 (it was originally B2 when I attached the example spreadsheet for help with writing the code), which is where I enter the # of goods being shipped. I'm not sure what the "With Range(a(i)) refers to, though I'm guessing I need to adjust this in some way to allow it to work?

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

    Re: VBA Code Troubleshooting

    What is the error message you are getting? What is the value of i when the error occurs? Can you attach your actual Excel file so we can try to run the code?

    I'm not sure what the "With Range(a(i)) refers to
    a appears to be an array that contains a list of range names. (It is a best practice to declare a, which was not done here.) The loop goes through every value of a, so Range(a(i)) refers to the range in the worksheet named by the corresponding name in the array.

    The most obvious possibility is that one (or more) of the names is not actually defined on your sheet.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Re: VBA Code Troubleshooting

    Range a is declared in the module code, as shown below:
    Please Login or Register  to view this content.
    So would I need to declare for a again in the private sub for my "Input" tab?

    Unfortunately I can't attach my main spreadsheet since it has far too much confidential information, but the attached is what the code was created on. The set up is basically the same in my main spreadsheet, only with far more "document" sheets, and far more cell for inputting information in the "Input" tab.
    Attached Files Attached Files
    Last edited by AliGW; 05-16-2024 at 10:19 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

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

    Re: VBA Code Troubleshooting

    a is not a range. It is a list of range names. If you go in your worksheet to Formulas, Name Manager you will see a list of names. In the code in the version of the file you just attached, a is defined as
    Please Login or Register  to view this content.
    All of those names are listed in Name Manager.

    You need to look in the file that has the actual code that you provided in the first post (the file you can't share), go to Formulas, Name Manager, and see if the names are listed in that file.
    Please Login or Register  to view this content.
    If any are missing, it is causing the error.

  5. #5
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Re: VBA Code Troubleshooting

    Thanks for trouble shooting this with me! So I've gone into my name manager, and every one of them is listed there. There are of course several other names related to other ranges in my name manager, though I don't want those to be a part of this code or to be hidden. I am still getting the "Ru-Time error '1004': Method 'Range' of object'_Global" failed". When I debug it, it continues to point to the "With Range(a(i))" line in the code.

    How can I tell what the value of i is when I run the code?
    Last edited by AliGW; 05-16-2024 at 10:18 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

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

    Re: VBA Code Troubleshooting

    When you click the Debug button that line of code will be highlighted in yellow. If you hover the cursor over the i anywhere in the code, you should get a little pop-up window telling you the value.

    If that doesn't work you can bring up the Immediate window with CTRL+g and then type
    ?i
    in the Immediate window. It will tell you the value.

    This will tell us which named range is causing the problem. However, you said they are all there so even if we can point to one, I'm not sure how to tell what is wrong with it.

    Is it possible for you to delete all the data and attach the file? I don't really need real data to troubleshoot this particular problem.

  7. #7
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Re: VBA Code Troubleshooting

    OK I can do that! The value of i appears to be 1, not sure if that helps at all. Here is the document! The reference cell is $I$21 in the "Input" tab (I highlighted it yellow). Once you try to change that value the error pops up immediately.
    Attached Files Attached Files
    Last edited by 6StringJazzer; 05-16-2024 at 05:52 PM. Reason: no need to quote an entire post when replying directly to it

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,960

    Re: VBA Code Troubleshooting

    PL_HIDE1 refers to this range:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Change this to just:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  9. #9
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    132

    Re: VBA Code Troubleshooting

    Quote Originally Posted by TMS View Post
    PL_HIDE1 refers to this range:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Change this to just:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Wow I Was sure I checked all of these ranges! That solved it. Thank you so much! I'm very disappointed that it was such a simple fix and I didn't catch that. Regardless, I'm very happy everything works now.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,960

    Re: VBA Code Troubleshooting

    You're welcome. Thanks for the rep.

    Guess you just checked they existed, not necessarily what they referred to

+ 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. Troubleshooting For Each loop
    By Exceloof in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2019, 05:04 PM
  2. [SOLVED] Troubleshooting - Prevent Duplicate Entries From VBA Code
    By mcodden in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2016, 11:40 AM
  3. Troubleshooting the 'if' function
    By mitchellkerr in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-28-2014, 10:39 AM
  4. [SOLVED] IF statement troubleshooting
    By MARKSTRO in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-14-2012, 08:30 PM
  5. [SOLVED] Deleting Duplicate Row code troubleshooting
    By ds16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2012, 04:31 PM
  6. Troubleshooting: Code works in some instances, but not all....
    By JP777 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-26-2012, 11:51 PM
  7. Troubleshooting code for export to text file.
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-03-2008, 07:44 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