+ Reply to Thread
Results 1 to 18 of 18

Hide and unhide rows based on referenced cell

  1. #1
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Hide and unhide rows based on referenced cell

    I'm using this vba code to hide rows and B3 is reference cell resulted from vlookupvalue which brings values from another sheet. The code is not working. File attached for your review.


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(False, False) = "B3" Then
    Select Case Target.Value
    Case "1": Rows("11:13").Hidden = True: Rows("9:10").Hidden = False
    Case "2": Rows("9:12").Hidden = True: Rows("13").Hidden = False
    Case "3": Rows("10:12").Hidden = True: Rows("9").Hidden = False
    Case "4": Rows("11:12").Hidden = True: Rows("10").Hidden = False: Rows("9").Hidden = True
    Case "5": Rows("9:11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True
    Case "6": Rows("11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True: Rows("10").Hidden = True
    Case "7": Rows("11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True: Rows("9").Hidden = True
    End Select
    End If
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    Hi,

    I'm just wondering if I'm looking at the wrong thing, but the rows are hiding without issue for me...

    The only thing I can think of is that perhaps you have macros disabled...?

    Let me know

  3. #3
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    Hi ajryan88,

    Thank you for your reply. The enable macro and trust access to vba project are checked. If you change the value in cell "F4" , does it hide some of the rows? In my case it doesn't. By the way, I'm using excel 2010.
    Thank you for your help.

    Cheers,
    Last edited by Macondo73; 08-14-2013 at 07:28 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    I didn't try to change cell F4, sorry. After looking at your code, the code indicates that I need to change cell B3 in order to hide/unhide any rows. Perhaps this is what is going wrong - try changing the value of B3 and see if you get the result that you are after.

    Let me know how you go with this, and if you still can't figure it out I'll take a look at the spreadsheet next time I'm on my computer.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Hide and unhide rows based on referenced cell

    the change event is not triggered by a formula recalculation so as mentioned you need to monitor the input cell (F4) instead
    Please Login or Register  to view this content.
    or use the worksheet_calculate event
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    ajryan88; Yes. The code says B3. But B3 is related to e2 and e2 has a vlookup looking into f4. The idea hide/unhiden will happen seamless without me pressing enter. It will change based on the lookup in cell E2. If you have a chance check the spreadsheet.


    Joseph;
    i updated the code using Calculate. It stops working. Would you please review the code and let me know what i'm doing wrong. Thank you for your assistance

    Private Sub Worksheet_Calculate(ByVal Target As Excel.Range)
    If Target.Address(False, False) = "B3" Then
    Select Case Target.Value
    Case "1": Rows("11:13").Hidden = True: Rows("9:10").Hidden = False
    Case "2": Rows("9:12").Hidden = True: Rows("13").Hidden = False
    Case "3": Rows("10:12").Hidden = True: Rows("9").Hidden = False
    Case "4": Rows("11:12").Hidden = True: Rows("10").Hidden = False: Rows("9").Hidden = True
    Case "5": Rows("9:11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True
    Case "6": Rows("11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True: Rows("10").Hidden = True
    Case "7": Rows("11").Hidden = True: Rows("12").Hidden = False: Rows("13").Hidden = True: Rows("9").Hidden = True
    End Select
    End If
    End Sub

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Hide and unhide rows based on referenced cell

    the Calculate event does not have a Target argument. I suggest you simply change your previous code to monitor F4

  8. #8
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    The value that will change is E2. E2 is a vlookup(E1,E:F,2). I need to monitor E2 as user will select different values in E1

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Hide and unhide rows based on referenced cell

    then I suggest you monitor E1 since that is the cell that is being directly changed-i.e. the cell that Target represents

  10. #10
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    Hi Joseph, Do you mind taking a look of the attachment.This file provide a better understanding of what i'm trying to accomplish.

    Thank you again for all your help...
    Attached Files Attached Files

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Hide and unhide rows based on referenced cell

    it would have been very useful to know you were using a cell linked to a forms control at the start-that will not trigger the change event! I suggest you assign a macro to the spinner instead

  12. #12
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    Try this.

    The macro now looks for a change in F4 and references the value in B3 when that change occurs. It seems to be working fine now when you change F4.

    Let me know
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    hi ajryan88,
    Would you please take a look of this file. Your code works well in the sample file but not in the file where i have the data. Do you mind?
    Thank you so much for your time and patience. I'm new to vba...
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    No problem. I will let you know how I go a bit later today

  15. #15
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    This works fine for me again, the only thing I can comment on is perhaps that you haven't defined whether row 12 should be hidden or unhidden in the case that cell B3 is changed to "3".

    If fixing this doesn't resolve your issue, could you please describe exactly what error(s) you are encountering (i.e. when you change what cell to what value, what do you expect to see and what actually happens instead - answer each of these points for each and every error that you come across).

    Hope this resolves your issue.

    Btw, when you are happy with the solution, please do not forget to mark the thread as solved and also click the * to the bottom-left of my post to say thanks

  16. #16
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    Ajryan88,

    Thank you for all your help. It works.

  17. #17
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Hide and unhide rows based on referenced cell

    You're very welcome.

    Btw, when you are happy with the solution, please do not forget to mark the thread as solved and also click the * to the bottom-left of my post to say thanks

  18. #18
    Registered User
    Join Date
    05-16-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Hide and unhide rows based on referenced cell

    Thank you Joseph

+ 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. how To hide/unhide rows based on cell value
    By novice2430 in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 06-05-2013, 01:39 PM
  2. Hide rows based on multiple ranges not containing certain value of referenced cell
    By markreisberg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2013, 05:26 PM
  3. Hide/Unhide rows based on cell value in each row
    By Drewf2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-22-2013, 07:11 AM
  4. Hide/Unhide rows based on Cell Value
    By noelwri in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2012, 04:25 PM
  5. Unhide / Hide Rows Based on Cell Value
    By bradaresnick in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 03-02-2010, 10:02 AM

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