简体   繁体   中英

Excel Macros to hide rows

I don't know how to write a macro in Excel, but I think I've found an answer on your site that gets me close to what I need. Here's my situation:

If cell K23 = cell T20 then do nothing.  
If cell K23 = cell (T21:T23) then hide rows 25:65

Here's the macro I have so far:

Sub HIDE()
    If Range(K23) = Range(T20) Then nil = True
    Else
    If Range(K23) = Range("T21:T23") Then Rows("25:65").EntireRow.Hidden = True

    End If: End If:

End Sub

I get an error that says "Else without If"

Please tell me what I've done wrong.

Thanks.

You need to put the "nil = true" on a separate line; if you have the statement to be executed on the same line as the "if", Excel VBA considers this to be the end of the "if" statement.

So you'd need to do:

Sub HIDE()
    If Range(K23) = Range(T20) Then 
        nil = True
    Else
        If Range(K23) = Range("T21:T23") Then 
            Rows("25:65").EntireRow.Hidden = True
        End If
    End If
End Sub

It's also a little easier to read this way. As commented on your question, you'll need to provide a bit more detail in order for us to help you get your function actually doing exactly what you need it to do.

One way...

Sub tgr()

    Range("25:65").EntireRow.Hidden = (WorksheetFunction.CountIf(Range("T21:T23"), Range("K23").Text) > 0)

End Sub

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