简体   繁体   English

Excel VBA代码在要隐藏的范围内添加新行时更新隐藏/取消隐藏行功能

[英]Excel VBA code to update the hide/unhide rows functions when a new row is added within the range wanted to be hidden

I currently have a code as follows to hide a range of cells when an activeX checkbox is clicked: 我目前有一个代码,当单击activeX复选框时可以隐藏一定范围的单元格:

Version 1: 版本1:

Private Sub CheckBox1_Click()

    If CheckBox1.Value = True Then

    Rows("14:30").Hidden = False

    Else

    Rows("14:30").Hidden = True

    End If

End Sub

Private Sub CheckBox2_Click()

    If CheckBox2.Value = True Then

    Rows("32:38").Hidden = False

    Else

    Rows("32:38").Hidden = True

    End If

End Sub

Private Sub CheckBox3_Click()

    If CheckBox3.Value = True Then

    Rows("40:54").Hidden = False

    Else

    Rows("40:54").Hidden = True

    End If

End Sub

Version 2: 版本2:

Private Sub CheckBox1_Click()

    [14:30].EntireRow.Hidden = Not CheckBox1

End Sub

Private Sub CheckBox2_Click()

    [32:38].EntireRow.Hidden = Not CheckBox2

End Sub

Private Sub CheckBox3_Click()

    [40:54].EntireRow.Hidden = Not CheckBox3

End Sub

THE PROBLEM: 问题:

Both versions work fine BUT the problem is that when a new row is added within the ranges specified, the row specifications obviously do not update as they are not variables. 两种版本都能正常工作,但问题是,当在指定范围内添加新行时,行规范显然不会更新,因为它们不是变量。

NOTE : There are more than 3 ActiveX checkboxes. 注意 :有超过3个ActiveX复选框。 I've got about 19. 我大约19岁。

QUESTION

I know the ranges need to be put to an integer or a variable but I am only new to VBA and not even a programmer (studied a few programming applications in college 4 years ago so can read code a bit) so I have no idea how to do it. 我知道范围需要输入整数或变量,但我只是VBA的新手,甚至都不是程序员(4年前在大学里研究了一些编程应用程序,因此可以读一些代码),所以我不知道该怎么做去做吧。 Currently working on automating an excel file at work and WOULD LOVE YOUR HELP PLEASEEEE! 目前正在致力于在工作中自动执行excel文件,并且会喜欢您的帮助! I have been struggling for days on this :( 我已经为此奋斗了几天:(

Excel file looks like this: Excel文件如下所示:

剪辑1

剪辑2

Thank you in advance!! 先感谢您!!

You need to dynamically determine the starting row and end row for each section. 您需要动态确定每个部分的开始行和结束行。 This should work for the top A - General section. 这应该适用于顶部A-常规部分。

Option Explicit

Private Sub CheckBox1_Click()

    Dim m1 As Variant, m2 As Variant

    m1 = Application.Match("A -*", Columns(1), 0)
    m2 = Application.Match("B -*", Columns(1), 0)

    If IsError(m1) Or IsError(m2) Then Exit Sub

    With Range(Cells(m1 + 1, "A"), Cells(m2 - 1, "A"))
        .EntireRow.Hidden = Not CheckBox1.Value
    End With

End Sub

在此处输入图片说明

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

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