+ Reply to Thread
Results 1 to 11 of 11

Need help with code that isn't working as intended

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

    Lightbulb Need help with code that isn't working as intended

    With the help of GPT (and you guys), I wrote some VBA code that will automatically hide rows in another sheet depending on whether the answer is "Yes" or "No" in my current sheet.

    My "Input Fields" tab has a list of parts. I can either select "Yes" or "No" for each part.

    The "Commercial Invoice" sheet lists every single part by default, but if I select "No" to one of the parts in my "Input Fields" tab, I want the row in my "Commercial Invoice" tab to be completely hidden, automatically.

    The macro for the HideRows() sub works perfectly fine when I run it manually, but the Module code does not, since the rows aren't being hidden automatically. The rows will only hide if I run the macro, but I want it to happen seamlessly. Here's the module code I am using:

    Please Login or Register  to view this content.
    Can you guys see any glooming issues with this code? I've no idea why I can't get it to work.

  2. #2
    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
    48,595

    Re: Need help with code that isn't working as intended

    If you want it to execute automatically, you'd need to use a Worksheet Change Event handler.
    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


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

    Re: Need help with code that isn't working as intended

    Quote Originally Posted by TMS View Post
    If you want it to execute automatically, you'd need to use a Worksheet Change Event handler.
    So I did initially have the subroutine named that "Worksheet_Change", but I already have another one of those set up for a different task.

    How would I go about adding a second, or adding this into my code? I already have some code that changes things automatically in my workbook that works swimmingly, so I was confused why this one wouldn't.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Need help with code that isn't working as intended

    Can you post an example file with both codes in it to test.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

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

    Re: Need help with code that isn't working as intended

    Quote Originally Posted by bakerman2 View Post
    Can you post an example file with both codes in it to test.
    Yes of course! Here it is. I deleted everything else that doesn't matter, and just left the toggles and everything needed to make this work.

    I've tested the macro, and it still works fine when run manually, but it doesn't happen automatically like I want it to. I've also added the same named-ranges and all that jazz.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,971

    Re: Need help with code that isn't working as intended

    Please Login or Register  to view this content.
    Ben Van Johnson

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

    Re: Need help with code that isn't working as intended

    Quote Originally Posted by protonLeah View Post
    Please Login or Register  to view this content.
    So when I use the "Sub Worksheet_Change" in this code I get an "Ambiguous name detected" error, presumably because I call out this exact wording in another piece of code that also hides rows. IS there anyway around this?

    Thank you so much for your help!!

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,971

    Re: Need help with code that isn't working as intended

    You didn't post the other code
    Please Login or Register  to view this content.
    Last edited by protonLeah; 12-18-2024 at 04:32 PM.

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

    Re: Need help with code that isn't working as intended

    Quote Originally Posted by protonLeah View Post
    You didn't post the other code
    Please Login or Register  to view this content.
    Ahhh sorry! I didn't think I needed it. Here it is!

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,971

    Re: Need help with code that isn't working as intended

    Combining the two codes:
    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: Need help with code that isn't working as intended

    Quote Originally Posted by protonLeah View Post
    Combining the two codes:
    Please Login or Register  to view this content.
    PERFECT!!! Works like a charm! I just had to swap the False & True statements (they were backwards), and delete the ' mark before the "Call AdjustRows(Target.Value)". Now everything works and happens automatically.

    Thanks so much for your help!

+ 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] Formulas not working as intended - not sure why
    By kmclean.wesco in forum Excel Formulas & Functions
    Replies: 26
    Last Post: 02-23-2024, 04:00 PM
  2. [SOLVED] IF OR statement not working as intended
    By chris01395 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2020, 09:50 AM
  3. PowerPivot not working as intended?
    By fingerstyle in forum Excel General
    Replies: 11
    Last Post: 10-22-2017, 08:35 PM
  4. [SOLVED] Msgbox not working as intended
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2013, 09:12 AM
  5. [SOLVED] Nested if formula not working as intended
    By Reibie in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-11-2013, 05:59 AM
  6. [SOLVED] VLOOKUP not working as intended
    By RAS 2112 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2013, 08:58 AM
  7. [SOLVED] macro not working as intended
    By Michael A in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2005, 10:06 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