Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
注册  \/ 
x

or

如何使用复选框在Excel中隐藏/取消隐藏行或列?

假设您需要使用复选框来隐藏或取消隐藏某些行或列。 例如,当Active X控件复选框被选中,则显示某些行或列,否则,它们将被隐藏。 本文向您展示了如何使用复选框来隐藏/取消隐藏Excel中行或列的详细信息。

使用复选框使用VBA代码隐藏/取消隐藏行或列


使用复选框使用VBA代码隐藏/取消隐藏行或列

以下VBA代码可以帮助您通过复选框隐藏/取消隐藏某些行或列。

1.将Active X Control复选框插入工作表后,右键单击该复选框,然后选择 查看代码 从右键单击菜单中。 看截图:

2。 在里面 Microsoft Visual Basic应用程序 窗口,将以下VBA代码复制并粘贴到“代码”窗口中。

VBA代码:使用复选框隐藏/取消隐藏行或列

Private Sub CheckBox1_Click()
    [C:D].EntireColumn.Hidden = Not CheckBox1
End Sub

说明:

1.在VBA代码中, [光盘]。 整列 表示通过选中或取消选中相应的复选框,可以隐藏或取消隐藏列C和D。

2.要隐藏或显示某些行(例如第6:9行),请更改[光盘]。 整列[6:9]。 整行 在VBA代码中

3。 按 其他 + Q 同时关闭按键 Microsoft Visual Basic应用程序 窗口。

4.现在请关闭 设计模式 在下面 开发商 标签,如下图所示:

从现在开始,选中该复选框时,将显示指定的行或列。 取消选中时,指定的行或列将被隐藏。


相关文章:


最佳办公效率工具

Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及您以前使用过的任何东西; 加密单元 带密码 创建邮件列表 并发送电子邮件...
  • 超级公式栏 (轻松编辑多行文本和公式); 阅读版式 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不会丢失数据; 拆分单元格内容; 合并重复的行/列...防止细胞重复; 比较范围...
  • 选择重复或唯一 行; 选择空白行 (所有单元格都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择...
  • 确切的副本 多个单元格,无需更改公式参考; 自动创建参考 到多张纸; 插入项目符号,复选框等...
  • 提取文字,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级滤镜 (将过滤方案保存并应用于其他工作表); 高级排序 按月/周/日,频率及更多; 特殊过滤器 用粗体,斜体...
  • 结合工作簿和工作表; 根据关键列合并表; 将数据分割成多个工作表; 批量转换xls,xlsx和PDF...
  • 超过300种强大功能。 支持Office / Excel 2007-2019和365。支持所有语言。 在您的企业或组织中轻松部署。 完整功能30天免费试用。 60天退款保证。
kte选项卡201905

Office选项卡为Office带来了选项卡式界面,使您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    etb1025 · 5 months ago
    What if i wanted to use 3 different checkboxes and wanted them to each toggle hiding different groups of rows? I have tried and looked all over. When I try to modify this code the checkboxes seem to interfere with each other.
  • To post as a guest, your comment is unpublished.
    Eric · 5 months ago
    What if i wanted to use 3 different checkboxes and wanted them to each toggle hiding different groups of rows? I have tried and looked all over. When I try to modify this code the checkboxes seem to interfere with each other.

  • To post as a guest, your comment is unpublished.
    Maggie · 1 years ago
    Very useful, but how can I use it to hide just a part of the spreadsheet? I mean for example cells B2:B11, how to combine those two functions? Thanks.
  • To post as a guest, your comment is unpublished.
    angela · 1 years ago
    It worked, but then when I saved the file, and reopen it doesn't work. I tried saving it as a Macro excel sheet and as a regular workbook. What have I done wrong? I want to give it to someone else to use and don't want them to be confused.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi angela,
      After adding the code into workbook, please click File > Save as, choose where to save the file, in the Save As dialog box, choose Excel Macro-Enabled Workbook (*.xlsm) from the Save as type drop-down, and finally click Save. Then send this Excel Macro-Enabled workbook to others.
  • To post as a guest, your comment is unpublished.
    May · 1 years ago
    I was try to unhide the first three role but it was not work.How can I do it ,I know it the person who send me that file wanna know my skill I can solve it or not.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi May,
      Sorry I didn't get your point.
  • To post as a guest, your comment is unpublished.
    Therese · 2 years ago
    Hello,

    Is there a way to hide only one row. If I put 6:9 all rows are hidden, but if I only put 6, it comes out as error. Would appreciate your help, thank you!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi,
      Please change the second line in the code to:
      [6:6].EntireRow.Hidden = Not CheckBox1.
      Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Lou · 2 years ago
    Hi,

    Hi, I've done the code, it's work good but when I try to make the same in more than one checkbox at the same sheet they work 'together', only hide or unhide the rows if all three checkboxes are selected.
    I am tryint to make these three checkboxes works independent one from another. And let the users mark only one box for at time.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Lou,
      Please make sure the checkboxes you created are ActiveX Controls checkboxes. These checkboxes works independently in your worksheet.
      Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Ed · 2 years ago
    Hello,

    I am trying to use this code to hide/unhide on a separate sheet from the checkbox. What do I have to add to make this work? Thanks in advance.

    Private Sub CheckBox1_Click()
    [C:D].EntireColumn.Hidden = Not CheckBox1
    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Good day,
      As the following code shown, please replace the "Sheet4" with your separate sheet name.

      Private Sub CheckBox1_Click()
      [Sheet4!C:D].EntireColumn.Hidden = Not CheckBox1
      End Sub
  • To post as a guest, your comment is unpublished.
    Cole · 3 years ago
    It seems like this code will only go one direction, i.e. only hide and not unhide them when clicked. Is there a way to correct this?
    • To post as a guest, your comment is unpublished.
      Kentthegreet · 3 years ago
      We have same problem but find a way to make it work. Please use ActiveX Controls checkbox and not at the Form Control checkbox. The code perfectly works with ActiveX checkbox.


      Private Sub CheckBox1_Click()
      [C:D].EntireColumn.Hidden = Not CheckBox1
      End Sub
    • To post as a guest, your comment is unpublished.
      Kentthegreet · 3 years ago
      We have same problem but I found out we're using Form Controls and not ActiveX Controls. The code perfectly works in ActiveX Control Checkbox button. Just turn off the Design Mode so you can click check the Checkbox.
  • To post as a guest, your comment is unpublished.
    Pieter Ramaut · 3 years ago
    @Wayne:

    Private Sub CheckBox1_Click()
    ActiveSheet.Unprotect Password:="xxxxx"
    Rows("284:351").EntireRow.Hidden = Not CheckBox1
    ActiveSheet.Protect Password:="xxxx"
    End Sub
  • To post as a guest, your comment is unpublished.
    Wayne Bailey · 4 years ago
    Is there a way to use the "Use checkbox to hide/unhide rows or columns with VBA code" on a protected worksheet?