+ Reply to Thread
Results 1 to 12 of 12

VBA Code to hide and unhide based on a cell value

  1. #1
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Wink VBA Code to hide and unhide based on a cell value

    I am trying to create a Macro that takes cell B2 (which is a formula, to show as either Law or Non Law) and hides lines 39-43 on the same sheet if B2 is "Non Law". Any assistance would be greatly appreciated.
    Last edited by Silver13; 03-23-2015 at 03:03 PM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to hide and unhide based on a cell value

    Do you want it to update when B2 changes? If so we need to know what cells and linked to the formula in B2. Or are you wanting to have a button that you can press that hides/unhides based upon value in B2?

  3. #3
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    Yes I need the rows to hide / unhide based on what is in B2. The formula in B2 is: =IF(CCBNonLawApplies<>"","Non Law",IF(CCBLawApplies<>"","Law",""))
    Basically, if the user selects a field in the first tab of the workbook, it will fill in B2 with Law or Non Law. I need the Macro to hide rows 39-43 of B2 shows Non Law. Trying to have this automatic to avoid the use of a button if possible.
    Thank you!

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to hide and unhide based on a cell value

    What is CCBNonLawApplies and CCBLawApplies? Note: The reason I am asking this is because the worksheet_change event will not recognize a change in formula so I need to track down what range actually gets changed.

  5. #5
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    It is on the first tab of my workbook. The user will either select applies in Law or Applies in Non Law both are name boxes.
    LAW_Non Law.jpg

    the selections are in B59 or D59. B59 the user selects "Applies" for Non Law in D59 the user can select "Applies" for Law. If the user selects Non Law on the GeneralInfo_ADMIN tab, then B2 on the SORA tab will show Non Law and the rows will need to be hidden. If the user selects Law on the GeneralInfo_ADMIN tab, then B2 on the SORA tab will show Law and the rows need to show.

    Thank you so much for your assistance!
    Last edited by Silver13; 03-20-2015 at 03:02 PM.

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to hide and unhide based on a cell value

    So by selecting "Applies" in cell B59 the formula in B2 should be updated to "Non-Law" at which time you want to hide lines 39-43?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    Yes, by selecting "Applies" in B59 of the prior tab (tab 1), B2 in the next tab (tab 2) will be updated to "Non Law" and at that time I need lines 39 - 43 to be hidden in tab 2. Thank you so much for all of your help @stnkynts.

  8. #8
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to hide and unhide based on a cell value

    Modify my submitted code to reference tab 2.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    I am getting a Compile error: Expected End Sub

  10. #10
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    I figured out the Compile Error and have updated the code as follows:

    Please Login or Register  to view this content.

    But the macro doesnt hide the rows when the event changes. I have tested it and selected "Non Law" and the rows are not hidden.

  11. #11
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Code to hide and unhide based on a cell value

    Submit your workbook

  12. #12
    Registered User
    Join Date
    02-19-2015
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code to hide and unhide based on a cell value

    I had the code in a module, instead of my sheet 1. I also updated the code to work based on D59 as well.

    Please Login or Register  to view this content.
    Thank you for all of your help with this!

+ 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. Vb code to hide/unhide rows based on entries on another sheet
    By tcrjmom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2015, 01:15 PM
  2. [SOLVED] vba code for hide/unhide tabs based on color
    By Allseasons in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 03-17-2014, 04:04 PM
  3. Hide / unhide cells based on a VBA code
    By Metalgijs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-08-2013, 11:21 AM
  4. Hide and Unhide sheets based on Date [better code]
    By Deryl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2012, 09:22 AM
  5. [SOLVED] How VB code can unhide/hide rows based on combobox selection?
    By jgomez in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-04-2012, 01:25 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