VBA根据单元格的值是否为零显示/隐藏行 [英] VBA to show/hide rows based on whether a cell's value is zero

查看:155
本文介绍了VBA根据单元格的值是否为零显示/隐藏行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表,我想根据另一个单元格中的值隐藏或取消隐藏某些行.

I have an Excel sheet where I would like to hide or unhide certain rows depending on the value in another cell.

简而言之:
整个过程应取决于单元格 C2 D2 E2 中的值.

In Short:
The whole thing should depend on the value in cell C2, D2, E2.

如果 C2 空白,如果不是空白,我想隐藏第31至40行,则必须可见.

If C2 is blank I would like rows 31 to 40 be hidden, if it is not blank, they need to be visible.

其他三个单元格相同,始终隐藏/取消隐藏以下10行:

The same for the other three cells, always hiding/unhiding the following 10 rows:

D2 --> rows 41 to 50
E2 --> rows 51 to 60

我尝试了此代码,但是它不起作用,并且我也没有收到任何错误:

I tried this code but it is not working and I do not get any error either:

Sub Hide_rows()
    If Range("LS!C2") = 0 Then
        Rows("31:40").EntireRow.Hidden = True
    Else
        If Range("LS!D2") = 0 Then
            Rows("41:50").EntireRow.Hidden = True
        Else
            If Range("LS!E2") = 0 Then
                Rows("51:60").EntireRow.Hidden = True
            Else
                If Range("LS!C2") > 0 Then
                    Rows("31:40").EntireRow.Hidden = False
                Else
                    If Range("LS!D2") > 0 Then
                        Rows("41:50").EntireRow.Hidden = False
                    Else
                        If Range("LS!E2") > 0 Then
                            Rows("51:60").EntireRow.Hidden = False
                        Else

                        End If
                    End If
                End If
            End If
        End If
    End If
End Sub

谢谢!

推荐答案

在我看来,您只需要以下几行:

Looks to me like you just need the following lines:

With Sheets("LS")
        .Rows("31:40").EntireRow.Hidden = (.Range("C2") = 0)
        .Rows("41:50").EntireRow.Hidden = (.Range("D2") = 0)
        .Rows("51:60").EntireRow.Hidden = (.Range("E2") = 0)
End With

按照Chris的观点进行

编辑-以下内容就足够了:

EDIT as per Chris's point - the following will suffice:

With Sheets("LS")
        .Rows("31:40").Hidden = (.Range("C2") = 0)
        .Rows("41:50").Hidden = (.Range("D2") = 0)
        .Rows("51:60").Hidden = (.Range("E2") = 0)
End With

这篇关于VBA根据单元格的值是否为零显示/隐藏行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆