简体   繁体   中英

Formula to hide rows based on the value of a cell

I have a worksheet that contains the names of all managers and their employees, ideally the way this sheet needs to work is that there is a drop down in the top left and when a manager selects their name, all rows that don't have their name against, are hidden, so only their team is shown.

I know auto filtering and having them choose their name would be the easiest way and is a good option to fall back on, but I'm hoping there is a way to do this with VBA or a formula to just hide rows when its not their team when they select their name in the drop down. As i'm trying to create something that's quite slick and looks nice

I did try to do something around having a helper cell to display true and false if the names matched, but came a bit stuck at this point. Tried using the code below, but it doesn't seem to actually be doing anything. Column with TRUE/FALSE is in Col A

Sub TEST()
Dim cell As Range

Application.ScreenUpdating = False
Application.EnableEvents = False
For Each cell In Range("A4:A34")
If cell.Value = "FALSE" Then
cell.EntireRow.Hidden = True
Else
cell.EntireRow.Hidden = False
End If
Next
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

Any ideas on how to do this without just using autofilter would be great

Given the following assumptions:

  • Drop down with Manager name is in cell A1
  • Column listing manager name for each row is in column A
  • Data set starts in row 5
  • Column A is contiguous with no blank spaces

Place the following code into the Worksheet module of the data sheet and change assumptions to fit your data set.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$1" and Target.Cells.Count = 1 Then

        Application.ScreenUpdating = False

        Range("A5:A1000").EntireRow.Hidden = False

        Dim mgrList as Range
        Set mgrList = Range(Range("A5"),Range("A5").End(xlDown))

        Dim mgrCheck as Range
        For each mgrCheck in mgrList
            mgrCheck.EntireRow.Hidden = mgrCheck <> Target
        Next

    End If

End Sub

Use an if then else statement with a call that shows/hides the rows that you'd like to show.

If Range("A1").Value = "John Snow" Then
Call Show_John_Snow
Else
If Range("A1").Value = "Daenerys Targaryen" Then
Call Show_Daenerys
Else....

'subroutine

Show_John_Snow
Rows("17:20").EntireRow.Hidden = True 'hide others
Rows("21:53").EntireRow.Hidden = False 'show John Snow
Rows("54:75").EntireRow.Hidden = True  'hide others

I have this data, in which I have Headers at A3:D3, data starting from row 4 to 99.
I tried applying Autofilter, check if this one works for you.

Sub test()

    Range("A3:D3").Select
    Selection.AutoFilter
    ActiveSheet.Range("A3:D99").AutoFilter Field:=2, Criteria1:="0"
    ActiveSheet.Range("A3:D99").AutoFilter Field:=2, Criteria1:="1"
End Sub

Here, I selected option named "0" from drop-down filter from Field-2, that is Range A4, and as you told, other cells automatically gets hidden, and the cells corresponding to that criteria only gets visible.
Also I tried with other option "1".

This seems a very difficult or involved way to do this, I have to show students their results without them seeing other students results.

So, one sheet has all the data and on a "front" sheet I call the relevant data for the particular student using index() and match(). Each student has an ID number which is entered, then for confirmation the name is returned then the relevant grades.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM