Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3745|回复: 5

[已解决]谢谢amulee老师,如何优化这段VB?工作表切换隐显

[复制链接]
发表于 2010-8-25 12:53 | 显示全部楼层 |阅读模式

无附件:

代码如下:

Sub 显示()'两类工作表的互相切换显示与隐藏

Application.ScreenUpdating = False

   Set Sht1 = Sheets("规则")
   Set Sht2 = Sheets("红")
   Set sht3 = Sheets("h1")
   Set sht4 = Sheets("轮盘")
   Set sht5 = Sheets("蓝")
   Set Sht6 = Sheets("w1")
   Set Sht7 = Sheets("★类型★")
   Set sht8 = Sheets("红D")
   Set sht9 = Sheets("h2")
   Set sht10 = Sheets("蓝D")
   Set Sht11 = Sheets("★热门★")
   Set Sht21 = Sheets("金浪")
   Set Sht12 = Sheets("3d首页")
   Set sht13 = Sheets("3d")
   Set sht14 = Sheets("3D_排列")
   Set sht20 = Sheets("3D图示")
   Set sht15 = Sheets("首页")
   Set Sht16 = Sheets("明细")
   Set Sht17 = Sheets("汇总")
   Set sht18 = Sheets("A")
   Set sht19 = Sheets("数据源")
   Set Sht22 = Sheets("B")
   Set sht23 = Sheets("C")
   Set sht24 = Sheets("D")
   Set sht25 = Sheets("E")
   Set sht26 = Sheets("板块")
   Set sht27 = Sheets("F")
   Set sht28 = Sheets("G")
   Set sht29 = Sheets("H")
'对两类工作表以切换的方式进行隐藏
   If sht15.Visible = True Then
   'A类工作表
      Sht12.Visible = False
      sht13.Visible = False
      sht14.Visible = False
      sht20.Visible = False
      sht15.Visible = False
      Sht16.Visible = False
      Sht17.Visible = False
      sht18.Visible = False
      Sht22.Visible = False
      sht23.Visible = False
      sht24.Visible = False
      sht25.Visible = False
      sht26.Visible = False
      sht27.Visible = False
      sht28.Visible = False
      sht29.Visible = False
   'B类工作表
      Sht1.Visible = True
      Sht2.Visible = True
      sht3.Visible = True
      sht4.Visible = True
      sht5.Visible = True
      Sht6.Visible = True
      Sht7.Visible = True
      sht8.Visible = True
      sht9.Visible = True
      sht10.Visible = True
      Sht11.Visible = True
      sht19.Visible = True
      Sht21.Visible = True


      ActiveSheet.CommandButton8.Caption = "显示MY-GP"
      Sheets("操作面").Select

   Else
   mm = InputBox("请输入密码", "此为我专用,请勿乱试!!")
If mm <> "123456" Then GoTo 100
   'A类工作表
      Sht12.Visible = True
      sht13.Visible = True
      sht14.Visible = True
      sht20.Visible = True
      Sht16.Visible = True
      Sht17.Visible = True
      sht18.Visible = True
      Sht22.Visible = True
      sht23.Visible = True
      sht24.Visible = True
      sht25.Visible = True
      sht26.Visible = True
      sht27.Visible = True
      sht28.Visible = True
      sht29.Visible = True
   'B类工作表
      Sht1.Visible = False
      Sht2.Visible = False
      sht3.Visible = False
      sht4.Visible = False
      sht5.Visible = False
      Sht6.Visible = False
      Sht7.Visible = False
      sht8.Visible = False
      sht9.Visible = False
      sht10.Visible = False
      Sht11.Visible = False
      sht19.Visible = False
      Sht21.Visible = False

      ActiveSheet.CommandButton8.Caption = "隐藏MY-GP"
End If
100:
      Sheets("操作面").Select
Application.ScreenUpdating = True
End Sub

老师:

不必完全拘泥于上面的代码,我只是想求得对这类型的用途的代码的简化方法,随着模板内工作表的增多,我觉得附上的这组代码很不简洁,

大致可以描述成这样:在一个模板内有许多工作表,其中[操作面]是永远也不需要隐藏的,余下的N张工作表按用途分成两类,权定为A类工作表和B类工作表

用户通过[操作面]里的按纽对这两组工作表进行切换显隐,即当A类工作表显示时,B类工作表隐藏,当A类工作表隐藏时,B类工作表显示,有没有比上面的代码更简洁和优化的代码?

[此贴子已经被作者于2010-8-25 21:01:43编辑过]
最佳答案
2010-8-25 13:07
Sub Test()
    Dim ArrA(1 To 3)    'A类工作表,这个数量可以自己定
    Dim ArrB(1 To 3)    'B类工作表,这个数量可以自己定
    Dim Sht
    '用数组给表格分组,记录各组的表名
    ArrA(1) = "Sheet1"
    ArrA(2) = "Sheet2"
    ArrA(3) = "Sheet3"
    ArrB(1) = "Sheet4"
    ArrB(2) = "Sheet5"
    ArrB(3) = "Sheet6"
    '隐藏A类表格
    For Each Sht In ArrA
        Worksheets(Sht).Visible = False
    Next
    '显示B类表格
    For Each Sht In ArrB
        Worksheets(Sht).Visible = True
    Next
End Sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2010-8-25 13:07 | 显示全部楼层    本楼为最佳答案   

Sub Test()
    Dim ArrA(1 To 3)    'A类工作表,这个数量可以自己定
    Dim ArrB(1 To 3)    'B类工作表,这个数量可以自己定
    Dim Sht
    '用数组给表格分组,记录各组的表名
    ArrA(1) = "Sheet1"
    ArrA(2) = "Sheet2"
    ArrA(3) = "Sheet3"
    ArrB(1) = "Sheet4"
    ArrB(2) = "Sheet5"
    ArrB(3) = "Sheet6"
    '隐藏A类表格
    For Each Sht In ArrA
        Worksheets(Sht).Visible = False
    Next
    '显示B类表格
    For Each Sht In ArrB
        Worksheets(Sht).Visible = True
    Next
End Sub
回复

使用道具 举报

 楼主| 发表于 2010-8-25 14:38 | 显示全部楼层

回复:(amulee)Sub Test()    Dim A...

amulee老师:

VB运行出错:下标越界

 

RZojVZaC.rar (9.76 KB, 下载次数: 0)
回复

使用道具 举报

发表于 2010-8-25 15:39 | 显示全部楼层

越界是因为表格名称不对,你根据自己需要修改呀。
回复

使用道具 举报

 楼主| 发表于 2010-8-25 19:07 | 显示全部楼层

回复:(amulee)越界是因为表格名称不对,你根据自己...

QUOTE:
以下是引用amulee在2010-8-25 15:39:00的发言:
越界是因为表格名称不对,你根据自己需要修改呀。

表格名称?

我不懂。我在第3楼上传了附件,请老师能否在附件中指出,谢谢

还有,你的VB里是不是必须要将所有的工作表进行赋值,能否只对A类工作表赋值?

而对于B类工作表可不可以以语句的方式来定义,这样每当用户在模板内增加B类工作表后,就不必再对这个VB进行修改?

回复

使用道具 举报

 楼主| 发表于 2010-8-25 21:01 | 显示全部楼层

回复:(amulee)越界是因为表格名称不对,你根据自己...

QUOTE:
以下是引用amulee在2010-8-25 15:39:00的发言:
越界是因为表格名称不对,你根据自己需要修改呀。

测试成功了,原先的理解有误,还以为sheet1是指VB里的名称,原来是指肉眼看得到的工作表表名,见笑了

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-16 17:52 , Processed in 0.223585 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表