简体   繁体   English

根据单元格中的单词在excel中隐藏行

[英]hiding rows in excel based on words in a cell

I am VERY new at VBA and have been searching for VBA code that can do what I want. 我是VBA的新手,一直在寻找可以做我想做的VBA代码。 I have tried several and attempted to adapt them however I just can't seem to get them right so thought I would try for some help! 我已经尝试了几种方法并尝试使它们适应,但是我似乎无法使它们正确,因此我想寻求帮助!

I have 6 projects that have either YES or NO in the cell next to them indicating if the person is working on that project. 我有6个项目,在它们旁边的单元格中有“是”或“否”,指示该人是否正在从事该项目。 This is determined by the persons name next to that project on another sheet, so is formula produced and not a drop down or typed in value. 这由另一页上该项目旁边的人员名称确定,因此公式是生成的,而不是下拉列表或键入值。

The project then has a few rows below corresponding to it. 然后,该项目下面对应几行。

If there is a NO next to a project (in C6), I want the corresponding rows for that project to be hidden (rows 13:29). 如果项目旁边(在C6中)没有NO,我希望该项目的相应行被隐藏(行13:29)。

I want this to be repeated for each project, 我希望对每个项目都重复一次

so a no in c7 hides 31:47, a no in C8 hides 49:65, a no in C9 hides 67:83, a no in C10 hides 85:101, a no in C11 hides 103:118, 因此c7中的否隐藏31:47,C8中的否隐藏49:65,C9中的否隐藏67:83,C10中的否隐藏85:101,C11中的否隐藏103:118,

I don't know if this is possible and have been going around in circles, really hope that someone can help :) 我不知道这是否可行,并且一直在兜圈子,真的希望有人可以提供帮助:)

this is one of the adaptions i have tried but i am sure i am doing something wrong, sorry for not posting this here before 这是我尝试过的一种适应方法,但我确定我做错了,对不起,您之前没有在此处发布此内容

Private Sub Worksheet_Change(ByVal Target As Range) 

If Target.Address = "$C$6" Then 
If Target.Value = NO Then 
Rows(13:29).EntireRow.Hidden = True 
Else 
Rows(13:29).EntireRow.Hidden = False 

If Target.Address = "$C$7" Then 
If Target.Value = NO Then 
Rows(31:47).EntireRow.Hidden = True 
Else 
Rows(31:47).EntireRow.Hidden = False 
End If 
End If 


If Target.Address = "$C$8" Then 
If Target.Value = NO Then 
Rows(49:65).EntireRow.Hidden = True 
Else 
Rows(49:65).EntireRow.Hidden = False 
End If 
End If 

If Target.Address = "$C$9" Then 
If Target.Value = NO Then 
Rows(67:83).EntireRow.Hidden = True 
Else 
Rows(67:83).EntireRow.Hidden = False 
End If 
End If 


If Target.Address = "$C$10" Then 
If Target.Value = NO Then 
Rows(85:101).EntireRow.Hidden = True 
Else 
Rows(85:101).EntireRow.Hidden = False 

End If 
End If 



If Target.Address = "$C$11" Then 
If Target.Value = NO Then 
Rows(103:119).EntireRow.Hidden = True 
Else 
Rows(103:119).EntireRow.Hidden = False 

End If 
End If 


End Sub 

Shortest code I can think of: 我能想到的最短代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Dim bHide As Boolean
    bHide = (InStr(1, Target.Value, "NO", vbTextCompare) > 0)
    Select Case Target.Address
        Case "$C$6"
            Rows("13:29").EntireRow.Hidden = bHide
        Case "$C$7"
            Rows("31:47").EntireRow.Hidden = bHide
        Case "$C$8"
            Rows("49:65").EntireRow.Hidden = bHide
        Case "$C$9"
            Rows("67:83").EntireRow.Hidden = bHide
        Case "$C$10"
            Rows("85:101").EntireRow.Hidden = bHide
        Case "$C$11"
            Rows("103:119").EntireRow.Hidden = bHide
    End Select
End Sub

Please test. 请测试。

so a no in c7 hides 31:47, a no in C8 hides 49:65, a no in C9 hides 67:83, a no in C10 hides 85:101, a no in C11 hides 103:118, 因此c7中的否隐藏31:47,C8中的否隐藏49:65,C9中的否隐藏67:83,C10中的否隐藏85:101,C11中的否隐藏103:118,

C11 hides 103:118 ? C11隐藏103:118吗? Shouldn't this be C11 hides 103:119 ? 这不是C11隐藏的103:119吗? Let me explain. 让我解释。

To make your work simple, you need to find a trend on how your code would progress. 为了使您的工作变得简单,您需要找到有关代码如何发展的趋势。 See this analysis. 请参阅此分析。

在此处输入图片说明

You are looking for "No" in row 6 and showing/hiding from row 13:29 . 您在第6行中寻找“否”,并在第13:29行显示/隐藏。 Similarly you are looking for "No" in row 7 and showing/hiding from row 31:47 . 同样,您要在第7行中查找“ No”,并从第31:47行显示/隐藏。 So if you notice then there is a trend. 因此,如果您注意到,则有趋势。 The difference between the row is 18 . 该行之间的差是18 See image above. 参见上图。 And If there is a trend then there is a possibility of a loop! 如果有趋势,那么就有可能出现循环!

And because of this simple table I could also figure out that what you said C11 hides 103:118 is incorrect and probably a typo. 而且由于这个简单的表格,我还可以弄清楚您说的C11 hides 103:118是错误的,并且可能是错字。 The 118 had to be 119 to maintain that difference of 18 118必须是119才能保持18差异

So your code can actually be condensed to (Untested) 因此,您的代码实际上可以压缩为(未经测试)

Dim i As Long, StartRow As Long, EndRow As Long

StartRow = 13
EndRow = 29

For i = 6 To 11
    If UCase(Range("C" & i).Value) = "NO" Then
        Rows(StartRow & ":" & EndRow).EntireRow.Hidden = True
    Else
        Rows(StartRow & ":" & EndRow).EntireRow.Hidden = False
    End If
    StartRow = StartRow + 18
    EndRow = EndRow + 18
Next i

The next thing is that since the NO's are getting changed by formula, you will have to use Worksheet_Calculate and not Worksheet_Change until and unless the formula is picking up values from the same sheet and the cells which are changing C6:C11 are manually changed by a user. 接下来的事情是,由于NO's由公式更改,因此您将不得不使用Worksheet_Calculate而不是Worksheet_Change直到并且除非公式从同一张表中获取值并且正在更改C6:C11的单元格都由a手动更改用户。

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

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