简体   繁体   English

Excel VBA代码可切换隐藏和取消隐藏行

[英]Excel VBA code to toggle hide and unhide rows

I have grouped few rows in my excel sheet. 我在Excel工作表中将几行分组。 I have added couple of buttons to hide and unhide the rows. 我添加了几个按钮来隐藏和取消隐藏行。 But I want to combine the code and have only one button which works more like a toggle button. 但是我想组合代码,只有一个按钮,其功能更像是切换按钮。 I am not able to achieve this. 我无法实现这一目标。 These are my individual functions. 这些是我的个人职能。

Sub Button1_Click()
    ActiveSheet.Outline.ShowLevels Rowlevels:=1
End Sub
Sub Button2_Click()
    ActiveSheet.Outline.ShowLevels Rowlevels:=2
End Sub

Any help with this is appreciated. 任何帮助,不胜感激。 Please note: I do not have any experience with VBA and I need this to be done for my maanger's requirement on one of his work. 请注意:我没有使用VBA的任何经验,我需要做这件事,以满足我的女主人对他的一项工作的要求。

Thanks 谢谢

Try this: 尝试这个:

Activesheet.rows(x & ":" & y).hidden=Not activesheet.rows(x & ":" & y).hidden

Where x to y are number of rows you want to hide. 其中x到y是要隐藏的行数。 For instance: 例如:

Sub Button1_Click()
    ActiveSheet.Rows("2:4").Hidden = Not ActiveSheet.Rows("2:4").Hidden
End Sub

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

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