简体   繁体   English

Excel宏查询:基于单元格值隐藏行

[英]Excel Macro Query: Hiding Rows based on cell value

Hi I've tried the below code but it doesn't seem to be working can anybody help me. 嗨,我已经尝试过下面的代码,但似乎没有任何人可以帮助我。 I just want to hide rows 3 & 4 when B1 has the text Delete . 当B1的文本为Delete时,我只想隐藏第3行和第4行。 But I want this to run automatically when Delete is entered into B1. 但是我希望在将Delete输入B1时自动运行。

Thanks 谢谢

码

Something like this should work 这样的事情应该工作

Option Explicit 'Very first line to ensure that variables are declared

Private Sub Worksheet_Change(ByVal Target As Range)
    'check if target address is B1
    If Target.Address(RowAbsolute:=False, ColumnAbsolute:=False) = "B1" Then
        ShowHideRows
    End If
End Sub

Public Sub ShowHideRows()
    If Range("B1").Value = "Delete" Then
        Rows("3:4").EntireRow.Hidden = True
        Rows("7:8").EntireRow.Hidden = False
    ElseIf Range("B1").Value = "Open" Then 'use ElseIf if possible like here
        Rows("3:4").EntireRow.Hidden = False
        Rows("7:8").EntireRow.Hidden = True
    End If
End Sub

Note: This is case sensitive. 注意:这是区分大小写的。 So if you type in B1 delete instead of Delete it will not run. 因此,如果您键入B1,则delete而不是Delete将不会运行。 To make it non case sensitive use LCase() like: 要使其不区分大小写,请使用LCase()例如:

LCase(Range("B1").Value) = "delete" 'string delete must be lower case!

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

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