Excel精英培训网

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

[已解决]初学VBA,帮忙看看以下代码为什么不能这样使用

[复制链接]
发表于 2011-12-28 21:48 | 显示全部楼层 |阅读模式
  1. Sub che()

  2. x1 = UserForm1.CheckBox2

  3. x2 = UserForm1.CheckBox3

  4. X3 = UserForm1.CheckBox4

  5. x4 = UserForm1.CheckBox5

  6. x5 = UserForm1.CheckBox6

  7. x6 = UserForm1.CheckBox7

  8. x7 = UserForm1.CheckBox8

  9. x8 = UserForm1.CheckBox9



  10. If x1 = True Then



  11. Call Module4.c1



  12. ElseIf x1 = True And x2 = True Then



  13. Call Module4.c2



  14. ElseIf x1 = True And x2 = True And X3 = True Then



  15. Call Module4.c3



  16. ElseIf x1 = True And x2 = True And X3 = True And x4 = True Then



  17. Call Module4.c4



  18. ElseIf x1 = True And x2 = True And X3 = True And x4 = True And x5 = True Then



  19. Call Module4.c5



  20. ElseIf x1 = True And x2 = True And X3 = True And x4 = True And x5 = True And x6 = True Then



  21. Call Module4.c6



  22. ElseIf x1 = True And x2 = True And X3 = True And x4 = True And x5 = True And x6 = True _

  23. And x7 = True Then



  24. Call Module4.c7



  25. ElseIf x1 = True And x2 = True And X3 = True And x4 = True And x5 = True And x6 = True _

  26. And x7 = True And x8 = True Then



  27. Call Module4.c8



  28. ElseIf x1 = False And x2 = False And X3 = False And x4 = False And x5 = False And x6 = False _

  29. And x7 = False And x8 = False Then



  30. Call Module4.del



  31. Else



  32. MsgBox "請按順序選擇!"



  33. End If

  34. End Sub

复制代码
有没有优化的余地呢
最佳答案
2011-12-28 22:23
Sub che()
    Dim x(1 To 8) As Boolean
    For i = 1 To 8
        x(i) = UserForm1.Controls("checkbox" & i + 1)
    Next

    i = 1
    While x(i)
        Application.Run "module4.c" & i
        i = i + 1
    Wend

    If i < 9 Then MsgBox "請按順序選擇!"
End Sub

发表于 2011-12-28 22:23 | 显示全部楼层    本楼为最佳答案   
Sub che()
    Dim x(1 To 8) As Boolean
    For i = 1 To 8
        x(i) = UserForm1.Controls("checkbox" & i + 1)
    Next

    i = 1
    While x(i)
        Application.Run "module4.c" & i
        i = i + 1
    Wend

    If i < 9 Then MsgBox "請按順序選擇!"
End Sub

回复

使用道具 举报

 楼主| 发表于 2011-12-28 23:10 | 显示全部楼层
Zipall 发表于 2011-12-28 22:23
Sub che()
    Dim x(1 To 8) As Boolean
    For i = 1 To 8

非常感谢,代码我慢慢研究
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-21 12:51 , Processed in 0.218462 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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