简体   繁体   English

根据单元格的值隐藏行的公式

[英]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. 我知道自动筛选并让他们选择名称是最简单的方法,并且是一个很好的选择,但是我希望有一种方法可以使用VBA或公式来隐藏行而不是隐藏行团队在下拉菜单中选择自己的名字时。 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 列TRUE / FALSE的列在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 带有经理名称的下拉列表位于单元格A1中
  • Column listing manager name for each row is in column A 每行的列列表管理器名称在A列中
  • Data set starts in row 5 数据集从第5行开始
  • Column A is contiguous with no blank spaces 列A是连续的,没有空格

Place the following code into the Worksheet module of the data sheet and change assumptions to fit your data set. 将以下代码放入数据表的Worksheet模块,并更改假设以适合您的数据集。

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 then else语句和显示/隐藏您要显示的行的调用。

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. 我有此数据,其中头位于A3:D3,数据从第4行到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. 在这里,我从字段2的下拉过滤器中选择了名为“ 0”的选项,即范围A4,并且正如您所告知的,其他单元格将自动隐藏,并且仅对应于该条件的单元格才可见。
Also I tried with other option "1". 我也尝试了其他选项“ 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(). 因此,一张纸具有所有数据,在“前”纸上,我使用index()和match()调用特定学生的相关数据。 Each student has an ID number which is entered, then for confirmation the name is returned then the relevant grades. 每个学生都有一个输入的ID号,然后为了确认姓名,返回了姓名,然后返回了相关的成绩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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