简体   繁体   English

根据命名的单元格范围值隐藏行?

[英]Hiding Rows based on named cell range value?

I would like to create a macro that looks at the value of a Named cell range and Hide/Unhide cells based on the value in that Named Cell. 我想创建一个宏来查看命名单元格范围的值,并基于该命名单元格中的值隐藏/取消隐藏单元格。 I have a data validated list that one can select (a) Yes and (b) No from, If the user Selects the Named Cell "Select"(Cell "A1") then (a) Yes then Rows 5-10 must be hidden, otherwise they must be unhidden. 我有一个经过数据验证的列表,可以从中选择(a)是,并且(b)否,如果用户选择命名单元格“选择”(单元格“ A1”),则(a)是,那么必须隐藏第5-10行,否则必须将其隐藏。 The macro Does not need to look and the whole cell Value, as in "(a) Yes", it must only look at "(a)" 宏不需要查看整个单元格的值,就像“(a)是”中那样,它只能查看“(a)”

This is my code so far but it produces an error that says 到目前为止,这是我的代码,但会产生一个错误,提示

Object variable or With block variable not set 未设置对象变量或带块变量

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Cell As Range

Cell = Range("Select")

If Target.Address = Cell Then
    Select Case Left$(Cell.Value, 3)
        Case "(a)"
            Rows("5:10").Hidden = True
    End Select
Else 
    Rows ("5:10").Hidden = False
End If
End Sub

How would one do something like this? 怎么会这样?

Change the following: 更改以下内容:

Cell = Range("Select")

into: 变成:

Set Cell = Range("Select")

Moreover, you don't get expected result if you compare: 此外,如果进行比较,您将无法获得预期的结果:

Target.Address = Cell

which never would be met. 永远不会被满足。 Try the following: 请尝试以下操作:

Target.Address = Cell.Address

And one more suggestion. 还有一个建议。 You could combine both if and select conditions into one: 您可以将ifselect条件合并为一个:

If Target.Address = Cell.Address And Left$(Cell.Value, 3) = "(a)" Then

Finally, remember that "(a)" <> "(A)" due to case sensitivity in VBA. 最后,请记住,由于VBA中的区分大小写,因此“(a)” <>“(A)”。

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

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