简体   繁体   中英

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. The macro Does not need to look and the whole cell Value, as in "(a) Yes", it must only look at "(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:

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

Finally, remember that "(a)" <> "(A)" due to case sensitivity in VBA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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