+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: Using Value in Cell to hide/unhide rows

  1. #1
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Question Using Value in Cell to hide/unhide rows

    Hi to all,

    I'm very new to excel and would like to know if there are any options for me to hide 2 rows(rows 1 and 2) based on specific values in a cell( say the char 'X'). The cell is located in row 3 (in cell A3)that would not be hidden. I was hoping if i can use the VBE editor to create a macro to do so.

    Thanks in advance for any help rendered.

  2. #2
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    I manage to make some progress. I used the following codes to perform the function

    Sub HIDEROWUSINGTEXT()
    If Range("A9").Value = 5 Then
    Rows("7:7").EntireRow.Hidden = True
    Else
    Rows("7:7").EntireRow.Hidden = False
    End If
    End Sub
    By having a value 5 in cell A9 and when i ran the macro, row 7 got hidden. However, i had to run the marco everytime i change the value in the cell. Maybe if anyone can help me modify the codes to hide/unhide row 7 every time i change it would be good...
    Last edited by maayub15; 02-02-2012 at 07:43 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Using Value in Cell to hide/unhide rows

    Hi

    You need to use a Worksheet_Change macro. This fires every time a cell on the sheet is changed.

    Open VBE, double click the worksheet you are interested in and use paste in the following code:
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$9" Then
     Call HIDEROWUSINGTEXT
     End If
     
    End Sub
    It checks which cell has changed and, if it is the one you are interested in, it calls your existing Sub.

    Cheers, Rob.

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Using Value in Cell to hide/unhide rows

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Hi royUK, thanks for the tip. Sorry as I was not sure on how to put it in the format, but your linked proved helpful. I will be more careful from this moment forward.

  6. #6
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Quote Originally Posted by rscsmith View Post
    Hi

    You need to use a Worksheet_Change macro. This fires every time a cell on the sheet is changed.

    Open VBE, double click the worksheet you are interested in and use paste in the following code:
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$9" Then
     Call HIDEROWUSINGTEXT
     End If
     
    End Sub
    It checks which cell has changed and, if it is the one you are interested in, it calls your existing Sub.

    Cheers, Rob.

    Rob,

    Your suggestion was spot on. Really greatful for your input.Thanks a million. By the way, can i check if it is possible to hide any particular cell or a few cell. The hidden function only seem to affect complete rows and columns.
    Last edited by maayub15; 02-02-2012 at 07:54 PM. Reason: typo error

  7. #7
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Using Value in Cell to hide/unhide rows

    You can't hide a cell on its own. Hide does only work on Rows, Columns, or Sheets. Depending upon your set up, you could format it with white text so that it is invisible. Conditional Formatting could be used for this.

    Other options off the top of my head are:
    move it to and from a column/row that is always hidden,
    put it on a separate sheet and report it on the current sheet (use an IF statement to decide whether it should be displayed or not),
    put it in a text box that can be hidden or not.

    Cheers, Rob.

  8. #8
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Hi Rob,

    Thanks for the solution. i didnt know so many options were available. by the way, I'm having a slight issue based on the codes that you have suggested when i applied to another worksheet. This other worksheet is named "NDE Report" and is reflected as "Sheet8(NDE Report)" on the VBE window panel. Also i have tweaked my original codes with yours to be used in this worksheet but it does not automate the hiding function.

    Sub MPI_CHK_BOX()
    
    If Range("O26").Value = X Then
    Rows("27:59").EntireRow.Hidden = True
    Else
    Rows("27:59").EntireRow.Hidden = False
    End If
    End Sub
    - the above code was put into the module2 code box( do i have to take note of the X? should i put it in ""? This made no difference to the macro when i mnaully click it)

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$0$26" Then
    Call MPI_CHK_BOX
    End If
    End Sub
    - This one was placed into the worksheet(Sheet8) code box
    Last edited by maayub15; 02-03-2012 at 02:13 AM. Reason: typo and extra details

  9. #9
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Hi rob, Sorry but i managed to figure out what was causing the issue- I had mistyped "O" with a "0" for your code suggestion. My bad. Anyway, you have been a great help. Thanks

  10. #10
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Using Value in Cell to hide/unhide rows

    You're welcome. Glad it's all working. As an aside, you can move the code from your Module straight in to the Change macro, and you can use a neat trick to decide whether to hide or not, by setting the Hidden to be the answer to the If statement:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$O$26" Then
    Rows("27:59").EntireRow.Hidden = (UCASE(Range("O26").Value) = "X")
    End If
    
    End Sub
    This works because (Range("O26").Value = "X") returns True or False - i.e. the same as you want to set for Hidden. Something else to watch is the case. By default "x" <> "X", so UCASE will ensure you get the expected result.
    Last edited by rscsmith; 02-03-2012 at 03:25 AM.

  11. #11
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Nicely put Rob, Thanks. I'd like to know if i can use the same priniciple to hide columns. I have tried to replace the words "rows" to "columns" but i'm getting errors. I figure i might have gotten my syntax incorrect, care to enlighten me once again?

  12. #12
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Oops... managed to solve the issue

    Sub Hide_Details()
    
    If Range("Z14").Value = "Yes" Then
    Columns("AA:AP").EntireColumn.Hidden = True
    Else
    Columns("AA:AP").EntireColumn.Hidden = False
    End If
    
    End Sub

  13. #13
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    I'm given another task today. I'm required to control the hiding of 200 rows depending on the input value given in a certain cell. This means that if the cell has a numerical value of 5, i should hide the other 195 rows. I'm guessing that using a "If" or a "for" loop might be needed. Can anyone guide me using a variable for the user input and some basic syntax to form the coding? I might be able to continue from there and post the updated codes here for all to see and learn.

  14. #14
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    213

    Re: Using Value in Cell to hide/unhide rows

    Hi Maayub

    You could use RESIZE to do this. Again, this would be easier with some more information...

    If you want to keep row 5 and hide rows 6:200 you could use:

    Sheets("Sheet1").Rows(6).Resize(195).Hidden = True
    Hope this is enough of a clue. Clearly, you can put formulae in the place of the 6 and 195 (e.g. Range("A1").Value, etc)

    Cheers, Rob.

  15. #15
    Registered User
    Join Date
    02-02-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Using Value in Cell to hide/unhide rows

    Hi Rob, Thanks for the tip. I did notice the rows disappearing, but they do not resize when i change the values. As per your suggestion, instead of using a constant value in the cell, I referenced the function directly to the cell. In doing so, i hoped that the resizing change automatically. But this was not the case.

    Sub ROWRESIZE()
    Sheets("Sheet1").Rows(1).Resize(Range("A13").Value).Hidden = True
    End Sub
    For the above code, When i set my a13 cell value as 5, rows 1- 5 are hidden. But when i change it to 3, nothing happen. I did include the part on the code that will effect any changes to the cell like you mentioned in your earlier posts, but nothing happen. Any idea what i'm doing wrong?

    Thanks, Rob.

    Ayub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0