简体   繁体   English

Excel宏隐藏行

[英]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. 我不知道如何在Excel中编写宏,但我想我已经在您的网站上找到了答案,可以让我更加接近所需。 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; 您需要将“ nil = true”放在单独的行上; 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. 如果您要在与“ if”相同的行上执行该语句,则Excel VBA会将其视为“ if”语句的结尾。

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

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

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