VBS |案例 - 选择问题

发布于 01-22 19:01 字数 857 浏览 2 评论 0 原文

我意识到这很愚蠢,但是我花了太多时间试图解决这个问题。我只需要这个块即可循环,直到键入“红色”,“红色”,“蓝色”或“蓝色”。 (我将脚本更改为颜色以简化可读性)。

如果有一种更好的方法来做到这一点,请详细说明。

mbb=MsgBox ("Hit No" ,4, "Start script")
If mbb=7 Then mbt=MsgBox ("Do you like Red or Blue?" ,32, "Choose color")
If mbt=1 Then SOM

Sub SOM
Do
ibb=InputBox ("Please type: Red or Blue")

Select Case ibb
Case "Red"
mbt=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "red"
mbu=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "Blue"
mbv=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case "blue"
mbw=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case Else
MsgBox "Please Type: Red or Blue"
End Select
Loop Until mbt OR mbu OR mbv OR mbw=1
End Sub

If mbt Or mbu=1 Then mbx=MsgBox ("Rouge" ,4, "Rouge")
If mbv Or mbw=1 Then mby=MsgBox ("Bleu" ,4, "Bleu")

I realize this is silly but I've spent too much time trying to figure this out. I just need this block to loop until either "Red", "red", "Blue", or "blue" is typed; (I changed the script to colors to simplify readability).

If there's a better way to do this please elaborate.

mbb=MsgBox ("Hit No" ,4, "Start script")
If mbb=7 Then mbt=MsgBox ("Do you like Red or Blue?" ,32, "Choose color")
If mbt=1 Then SOM

Sub SOM
Do
ibb=InputBox ("Please type: Red or Blue")

Select Case ibb
Case "Red"
mbt=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "red"
mbu=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "Blue"
mbv=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case "blue"
mbw=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case Else
MsgBox "Please Type: Red or Blue"
End Select
Loop Until mbt OR mbu OR mbv OR mbw=1
End Sub

If mbt Or mbu=1 Then mbx=MsgBox ("Rouge" ,4, "Rouge")
If mbv Or mbw=1 Then mby=MsgBox ("Bleu" ,4, "Bleu")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2025-01-29 19:01:37

您可以在案例语句中列出多个拼写:

Dim sColor
sColor = "red"

Select Case sColor
    Case "Red", "red", "RED"
        MsgBox "Color is Red"
    Case "Blue", "blue", "BLUE"
        MsgBox "Color is Blue"
    Case Else
        MsgBox "Color not found"
End Select

这不会处理“红色”,“红色”,“红色”,“红色”和“红色”值,因此更防弹的方法是使用 lcase ucase 正如Geert Bellekens所建议的那样,

Select Case UCase(sColor)
    Case "RED"
        MsgBox "Color is Red"
    Case "BLUE"
        MsgBox "Color is Blue"
    Case Else
        MsgBox "Color not found"
End Select

strcomp 函数可以是另一种选择,可以是另一个选择。 - 不敏感,但阅读不像选定的案例块那样容易:

If StrComp(sColor, "RED", vbTextCompare) = 0  Then MsgBox "Color is Red"
If StrComp(sColor, "BLUE", vbTextCompare) = 0  Then MsgBox "Color is Blue"

You can list multiple spellings in your Case statements:

Dim sColor
sColor = "red"

Select Case sColor
    Case "Red", "red", "RED"
        MsgBox "Color is Red"
    Case "Blue", "blue", "BLUE"
        MsgBox "Color is Blue"
    Case Else
        MsgBox "Color not found"
End Select

This will not handle "REd", "ReD", "reD", "rED" and "rEd" values so a more bullet-proof approach is to convert the case using LCase or UCase functions as Geert Bellekens suggests:

Select Case UCase(sColor)
    Case "RED"
        MsgBox "Color is Red"
    Case "BLUE"
        MsgBox "Color is Blue"
    Case Else
        MsgBox "Color not found"
End Select

The StrComp function could be another option to make the comparison case-insensitive but it is not as easy to read as a Select Case block:

If StrComp(sColor, "RED", vbTextCompare) = 0  Then MsgBox "Color is Red"
If StrComp(sColor, "BLUE", vbTextCompare) = 0  Then MsgBox "Color is Blue"
如梦 2025-01-29 19:01:37

我设法找到答案;我意识到,在我的代码的这一部分中,避免使用案例功能会更容易。新格式如下:

'simplified

Option Explicit
Dim a, x, y, w, z

Do
a=InputBox ("Please select hot or cold" ,0, "Type: ""Hot"" or ""Cold"" ")
If a="hot" Then x=MsgBox ("Hot selected" ,64, "Test")
If a="cold" Then y=MsgBox ("Cold Selected" ,64, "Test")
If a="Hot" Then z=MsgBox ("Hot selected" ,64, "Test")
If a="Cold" Then w=MsgBox ("Cold selected" ,64, "Test")
Loop Until x Or y Or z Or w=1

I've managed to find an answer; I realized it would be easier to avoid using the case function for this part of my code. The new format is as follows:

'simplified

Option Explicit
Dim a, x, y, w, z

Do
a=InputBox ("Please select hot or cold" ,0, "Type: ""Hot"" or ""Cold"" ")
If a="hot" Then x=MsgBox ("Hot selected" ,64, "Test")
If a="cold" Then y=MsgBox ("Cold Selected" ,64, "Test")
If a="Hot" Then z=MsgBox ("Hot selected" ,64, "Test")
If a="Cold" Then w=MsgBox ("Cold selected" ,64, "Test")
Loop Until x Or y Or z Or w=1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文