简体   繁体   English

Excel VBA自动隐藏/取消隐藏行-多种单元格选择

[英]Excel VBA auto hide/ unhide rows - mulitiple cell selection

I am trying to hide/unhide rows based on the list value selected in a cell (Yes/No). 我试图根据单元格中选择的列表值 (是/否) 隐藏/取消隐藏行

Scenario is that there are two drop-down lists (in Cell B1 and B4 ), when the value of Cell B1 is selected as 'No' , then rows 2 and 3 needs to be hidden else if value Yes is selected in B1 , then rows 2 and 3 to unhide. 场景是有两个下拉列表(在单元格B1B4 ),当单元格B1的值被选择为'No' ,则第rows 2 and 3需要隐藏,否则,如果在B1选择了是,则rows 2 and 3取消隐藏。

Similarly, when the value of Cell B4 is selected as 'No' , then rows 5 and 6 needs to be hidden else if value Yes is selected in B4 , then rows 5 and 6 to unhide. 同样,当单元格B4的值选择为'No' ,则需要隐藏第rows 5 and 6否则,如果在B4选择了“是”,则第rows 5 and 6取消隐藏。

I have a working code (as shown below) but when I select a different drop down selection (say I have selected b1 as No initially and then went to B4 to select No) then my hidden rows dont remain hidden anymore and the rows are displayed (inspite of the selection being selected as No). 我有一个有效的代码(如下所示),但是当我选择一个不同的下拉选择时(例如,我最初选择b1为No,然后转到B4选择No),那么我隐藏的行就不再保持隐藏状态,并显示行(尽管选择了“否”)。

My Code as follows. 我的代码如下。

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet_Change_A Target
    Worksheet_Change_B Target
End Sub  

Private Sub Worksheet_Change_A(ByVal Target As Range)
    Dim rng As Range      
    Set rng = Target.Parent.Range("B1") 
    If Target.Address <> Range("B1").Address Then 
        Exit Sub 
    Cells.EntireRow.Hidden = False
    Select Case Range("B1")   
        Case "Yes": 
            Range("2:3").EntireRow.Hidden = False
        Case "No": 
            Range("2:3").EntireRow.Hidden = True 
    End Select
End Sub  

Private Sub Worksheet_Change_B(ByVal Target As Range)
    Dim rng As Range
    Set rng = Target.Parent.Range("B4") 

    If Target.Address <> Range("B4").Address Then 
        Exit Sub
    Cells.EntireRow.Hidden = False
    Select Case Range("B4")  
        Case "Yes": Range("5:6").EntireRow.Hidden = False
        Case "No": Range("5:6").EntireRow.Hidden = True
    End Select
End Sub

Please note that I am a beginner in coding and trying to grow as I learn. 请注意,我是编码方面的初学者,并且在学习时会不断成长。

Your help is greatly appreciated. 非常感谢您的帮助。 Please do let me know of you need any clarifications with respect to the scenario explained. 请让我知道您需要对所说明的情况进行任何澄清。

Thanks. 谢谢。

I think you can simplify it thus, but I may not have fully understood your intentions as to hiding rows. 我认为您可以简化它,但是我可能还没有完全理解您隐藏行的意图。 If B1 is No, should rows 2 and 3 remain hidden irrespective of the value of B4? 如果B1为“否”,则无论B4的值如何,第2行和第3行是否都将保留?

I'm not sure what the rng variable was intended to do but I don't think you need it and you can apply a general rule to hiding/showing. 我不确定rng变量打算做什么,但是我认为您不需要它,可以将常规规则应用于隐藏/显示。

Also, why call two other procedures (you may have a good reason)? 另外,为什么还要调用另外两个过程(您可能有充分的理由)?

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$1" Or Target.Address = "$B$4" Then
    'Cells.EntireRow.Hidden = False
    Select Case Target.Value
        Case "Yes": Target.Offset(1).Resize(2).EntireRow.Hidden = False
        Case "No": Target.Offset(1).Resize(2).EntireRow.Hidden = True
    End Select
End If

End Sub

The issue is in the line Cells.EntireRow.Hidden = False . 问题出在Cells.EntireRow.Hidden = False When you change B1, that line first unhides everything (including rows 4 and 5), then decides whether to hide rows 2 and 3. 更改B1时,该行首先取消隐藏所有内容(包括第4行和第5行),然后决定是否隐藏第2行和第3行。

The variable rng is never used. 永远不会使用变量rng。

The following explicitly checks the relevant cells in each subroutine. 以下内容显式检查每个子例程中的相关单元格。 However, if you have expensive calculations, then you might want to include the line If Target.Address <> Range("B1").Address Then Exit Sub from your implementation. 但是,如果您的计算量If Target.Address <> Range("B1").Address Then Exit Sub ,则可能需要包括If Target.Address <> Range("B1").Address Then Exit Sub从实现中If Target.Address <> Range("B1").Address Then Exit Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet_Change_A Target
    Worksheet_Change_B Target
End Sub
Private Sub Worksheet_Change_A(ByVal Target As Range)
    Select Case Range("B1").Value
        Case "Yes": Range("2:3").EntireRow.Hidden = False
        Case "No": Range("2:3").EntireRow.Hidden = True
    End Select
End Sub
Private Sub Worksheet_Change_B(ByVal Target As Range)
    Select Case Range("B4").Value
        Case "Yes": Range("5:6").EntireRow.Hidden = False
        Case "No": Range("5:6").EntireRow.Hidden = True
    End Select
End Sub

I highly recommend indenting your code as I've shown, for readability. 出于可读性考虑,我强烈建议您缩进代码,如所示。 I used Range("B4").Value to explicitly show that I am using the cell contents. 我使用Range("B4").Value明确显示我正在使用单元格内容。 Lastly, the VBA debugger is your friend for tracking down issues like this. 最后,VBA调试器是您追踪此类问题的朋友。 Click on the line of code you are interested about (or just before it), then in the VBA menu click Debug->Toggle Breakpoint (or F9). 单击您感兴趣的代码行(或之前),然后在VBA菜单中单击“调试”->“切换断点”(或F9)。 Then run the macro and the debugger wil stop at that line of code. 然后运行宏,调试器将在该行代码处停止。 You can inspect variables by hovering over them, step through the code or out of the sub. 您可以通过将变量悬停在变量上,单步执行代码或移出子变量来检查变量。 Very useful in cases like yours where you are not sure what is causing the issue. 在您不确定导致问题的原因之类的情况下非常有用。

Edit: SJRs solution is more elegant, in that it is more easily extensible if you want to add more ranges to hide. 编辑:SJRs解决方案更优雅,如果您想添加更多范围来隐藏,它更容易扩展。

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

相关问题 Excel VBA代码可切换隐藏和取消隐藏行 - Excel VBA code to toggle hide and unhide rows Excel VBA代码(分配给按钮)可基于另一个工作表中的单元格值隐藏/取消隐藏行 - Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across another sheet 如果单元格值为0,则excel vba切换隐藏/取消隐藏多个工作表中的范围行 - excel vba toggle hide/unhide range rows across multiple sheets, if cell value 0 Excel VBA根据下拉验证列表选择隐藏和取消隐藏列 - Excel VBA to hide and unhide columns based on a dropdown validation list selection Excel VBA隐藏基于另一个单元格选择的行无法使代码正常工作 - Excel VBA to hide rows based on another cell selection can't get code to work 使用VBA根据在不同工作表的单独单元格中所做的选择来隐藏/取消隐藏表中的列(命名范围) - Use VBA to hide/unhide columns (named ranges) in a table based on selection made in a separate cell on a different sheet 在Excel 2010中隐藏或取消隐藏VBA的控件是正确的吗? - It is correct for hide or unhide a control by VBA in Excel 2010? 使用VBA在Excel中隐藏/取消隐藏特定对象 - Hide/unhide specific objects in Excel with VBA Excel VBA-隐藏/取消隐藏“ na”列 - Excel VBA - hide/unhide “na” columns 使用VBA隐藏/取消隐藏行-多个范围 - Hide/Unhide rows with VBA - multiple ranges
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM