简体   繁体   English

根据第一个和最后一个单元格的值隐藏行

[英]Hiding Rows Based on Value of First and Last Cell

I have a workbook that has a number of sheets that represent items, and these sheets have numerous options in them to mark attributes of these items. 我有一本工作簿,其中有许多工作表代表项目,这些工作表中有许多选项可以标记这些项目的属性。 There is also a central sheet that summarizes the items' attributes. 还有一个中央工作表,总结了项目的属性。

For examples sake lets say we have 2 sheets, one called Cat and one called Dog. 例如,假设我们有2张纸,一张叫猫,一张叫狗。 If you filled in the relevant options it would appear in the summary sheet like so (if you can imagine each line being a row in Excel): 如果您填写了相关选项,它将显示在摘要表中,如下所示(如果您可以想象每行在Excel中都是一行):


Cat


Name: Squiggles 名称:花体


Colour: Black 颜色:黑色


Legs: 4 腿数:4


Dog


Name: Woofers 名称:低音扬声器


Colour: White 颜色:白色


Legs: 4 腿数:4


At the moment you can use an 'Item Select' sheet with some tickboxes on to select which items appear in the final summary. 目前,您可以使用带有某些复选框的“项目选择”表来选择哪些项目显示在最终摘要中。 For the above example both Cat and Dog would be ticked, if one wasn't it wouldn't appear in the final summary. 对于上面的示例,Cat和Dog都将被打勾,如果不是,则不会出现在最终摘要中。

This happens at the moment by hiding the exact rows that the Cat summary would appear on. 目前,这是通过隐藏Cat摘要将在其上出现的确切行而发生的。 This means that if you add any other rows above that, the VBA code wouldn't change and it would hide the wrong rows. 这意味着,如果在其上添加任何其他行,则VBA代码不会更改,并且会隐藏错误的行。

If CatCheckBox.Value = True Then
    'Unhide the rows that the Cat summary is on
    Sheets("Final Summary").Range("A3:A32").EntireRow.Hidden = False
Else
    'Hide the rows that the Cat summary is on
    Sheets("Final Summary").Range("A3:A32").EntireRow.Hidden = True
End If

What I would like to happen is for the VBA code to hide the Cat summary dynamically. 我想让VBA代码动态隐藏Cat摘要。 The idea I had in mind would be to find the value 'Cat' in the sheet and another value say 'EndCat' and hide them rows and the ones in between. 我想到的想法是在工作表中找到值“ Cat”,另一个值说“ EndCat”,然后将它们隐藏在行之间。 How could I go about this? 我该怎么办?

Look into named ranges . 查看命名范围 You can name ranges of cells (use Formulas-->Name Manager) 您可以命名单元格范围(使用“公式”->“名称管理器”)

For example you could: name rows 2 to 5 as CAT name rows 6 to 9 as DOG name rows 10 to 13 as COW name rows 14 to 17 as HAMSTER 例如,您可以:将第2至5行命名为CAT名称,将第6至9行命名为DOG名称,将第10至13行命名为COW名称,将第14至17行命名为HAMSTER

Now any inserts on row 1 would force the range to be renumbered automatically. 现在,第1行上的所有插入都将强制范围自动重新编号。 Lets say you insert 5 rows before row 2. CAT is now (automatically) row 7 to 10, DOG is 11 to 14. 假设您在第2行之前插入了5行。CAT现在(自动)在第7至10行,DOG是11至14。

You can refer to these in code with 您可以在代码中参考这些

Sheets("Final Summary").Range("CAT").EntireRow.hidden = false 

Be aware, however that unprotected ranges of cells are at risk. 但是请注意,不受保护的细胞范围存在危险。 You can insert a Row in the middle of these above ranges and it would expand the range. 您可以在上述范围的中间插入行,这样会扩大范围。 eg If CAT is row 2 to 5, inserting a row at row 3 would make the CAT range now 2 to 6. 例如,如果CAT是第2至5行,则在第3行插入一行将使CAT范围现在变为2至6。

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

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