在vba msgbox中不使用goto循环的方法 [英] Way to not use goto loops in vba msgbox

查看:69
本文介绍了在vba msgbox中不使用goto循环的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种避免在VBA中使用GoTo循环的方法,因为我了解它们会导致严重的混乱和问题.我有一个用户 InputBox ,其中用户定义了一个变量,当前在if语句上有3个选项,if = Y ElseIf = N,否则转到开始.

I am trying to find a way to avoid using GoTo loops in VBA as i understand they can lead to serious confusion and issues. I have a user InputBox, where the user defines a variables, currently on the if statement there are 3 options, if = Y ElseIf = N and else GoTo Start.

但是,这在用户错误键入变量(例如Y#等)的情况下非常有用,但是当用户想要关闭输入框(即单击取消"或叉号)时,我当前遇到问题.

However this works great for the case where the user mistypes the variable i.e Y# etc, but i currently run into issues when the user wants to close the input box i.e clicks cancel or the cross.

所以我想知道是否有一个更巧妙的解决方案,还是我被这个问题困扰?

So i was wondering if there is a more elligent solution to this or am I stuck with this hicup?

下面是我的代码,这只是一个测试集,用于测试我要添加到我的主代码中的这一新功能.

My code is below, this is only a test set used to test this new feature i am adding to my main code.

Sub MsgBox_Test ()

Dim TestVariable As String

VariableEntrey: TestVariable = InputBox(" Y or N")
If TestVariable = "Y" Or TestVariable = "y" Then
    MsgBox ("Yeyy")
ElseIf TestVariable = "N" Or TestVariable = "n" Then
    MsgBox ("Awww")
Else: GoTo VariableEntrey
End If 

End Sub 

感谢您能够提供的帮助

推荐答案

Option Explicit

Sub MsgBox_Test()

    Dim TestVariable As String
    Dim done As Boolean

    Do
        TestVariable = InputBox(" Y or N")

        done = True            ' preload exit value

        If LCase(TestVariable) = "y" Then
            MsgBox ("Yeyy")

        ElseIf LCase(TestVariable) = "n" Then
            MsgBox ("Awww")

        ElseIf Len(TestVariable) > 0 Then

            done = False       ' abort exit

        End If

    Loop Until done

End Sub

这篇关于在vba msgbox中不使用goto循环的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆