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.
I manage to make some progress. I used the following codes to perform the function
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...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
Last edited by maayub15; 02-02-2012 at 07:43 PM.
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:
It checks which cell has changed and, if it is the one you are interested in, it calls your existing Sub.Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$9" Then Call HIDEROWUSINGTEXT End If End Sub
Cheers, Rob.
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)
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.
Last edited by maayub15; 02-02-2012 at 07:54 PM. Reason: typo error
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.
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.
- 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)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
- This one was placed into the worksheet(Sheet8) code boxPrivate Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$0$26" Then Call MPI_CHK_BOX End If End Sub
Last edited by maayub15; 02-03-2012 at 02:13 AM. Reason: typo and extra details
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
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:
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.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
Last edited by rscsmith; 02-03-2012 at 03:25 AM.
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?
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
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.
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:
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)Sheets("Sheet1").Rows(6).Resize(195).Hidden = True
Cheers, Rob.
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.
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?Sub ROWRESIZE() Sheets("Sheet1").Rows(1).Resize(Range("A13").Value).Hidden = True End Sub
Thanks, Rob.
Ayub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks