无附件: 代码如下: 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编辑过]
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
|