如何在VBA输入框的if语句中使用<>

问题描述

在输入框中,我试图让用户仅输入某些文本,在这种情况下,是每周,每月,每季度,每半年或每年。看起来很容易,但我只是不知道我要去哪里错了。

Do
        frequency = InputBox("Please enter your savings frequency.")
        If frequency <> "weekly" or "monthly" or "quarterly" or "semiannually" or "annually" Then
        MsgBox ("Must be weekly,monthly,quarterly,semiannually,or annually .")
        End If
    Loop While frequency <> weekly" or "monthly" or "quarterly" or "semiannually" or "annually"

解决方法

您需要分别测试每个条件。您可以使用变量来检查循环条件。

尝试以下代码:

Do
    Retry = False
    Saving_frequency = InputBox("Please enter your savings frequency.")
    If Saving_frequency <> "weekly" and Saving_frequency <> "monthly" and Saving_frequency <> "quarterly" and Saving_frequency <> "semiannually" and Saving_frequency <> "annually" Then
        MsgBox ("Must be weekly,monthly,quarterly,semiannually,or annually .")
        Retry = True
    End If
Loop While Retry
,

此行:

If Saving_frequency <> "weekly" or "monthly" or "quarterly" or "semiannually" or "annually" Then
'code
End If

应为:

If Saving_frequency <> "weekly" _ 
And Saving_frequency <> "monthly" _
And Saving_frequency <> "quarterly"_
And Saving_frequency <> "semiannually" _
And Saving_frequency <> "annually" Then _
'code
End If

每个子句需要单独处理,而不是作为一个整体来处理。

您的版本为:

如果“ Saving_frequency”不是不是 Array的成员(“每周一次”,“每月一次”,“每季度一次”,“半年一次”,“每年一次”)然后

与我的替代版本相同,替代版本使用And运算符。

使用相同条件测试循环。

或者,可以通过用户窗体中的下拉列表或单选按钮更好地处理这样的多个答案。

,

InputBox与Application.InputBox

代码

Option Explicit

' InputBox Function
Sub DoLoopQuickFix()
    
    Dim Saving_frequency As String
    Do
        Saving_frequency = _
          InputBox(Prompt:="Please enter your savings frequency.",_
                   Title:="Savings Frequency")
        Select Case Saving_frequency
            Case "weekly","monthly","quarterly","semiannually","annually"
                MsgBox "You chose '" & Saving_frequency & "'.",_
                       vbInformation,"Success"
                Exit Do
            Case Else
                MsgBox "Must be weekly," _
                     & "or annually .",vbExclamation,"Fail"
        End Select
    Loop

End Sub

' Application.InputBox Method
Sub DoLoop()
    
    Dim Saving_frequency As Variant
    Do
        Saving_frequency = Application _
          .InputBox(Prompt:="Please enter your savings frequency.",_
                    Title:="Savings Frequency",_
                    Type:=2)
        Select Case Saving_frequency
            Case "weekly","Success"
                Exit Do
            Case False
                MsgBox "You cancelled.",vbCritical,"Cancel"
                Exit Sub
            Case Else
                MsgBox "Must be weekly,"Fail"
        End Select
    Loop

End Sub
,

您有许多预定义的项目,您需要检查您的输入是否是这些项目之一。这是字典的经典案例。

例如

Private SavingsFrequency as Scripting.Dictionary

' Later in your Module

If SavingsFrequency Is Nothing Then PopulateSavingsFrequency


Do

    Dim mySavingFrequency as String
    mySavingFrequency = InputBox("Please enter your savings frequency.")

    If SavingFrequency.Exists(LCase$(mySavingFrequency)) Then Exit Do

    MsgBox ("Must be one of: " & GetSavingsFrequencyList())
       
Loop 

' Remainder of code

Public Sub PopulateSavingsFrequency()

    Set SavingsFrequency = New Scripting.DIctionary
    With SavingsFrequency

        .Add .Count,"weekly"
        .Add .Count,"monthly"
        .Add .Count,"quarterly"
        .Add .Count,"semiannually"
        .add .Count,"annually"

    end with

End Function

Private Function GetSavingFrequencyList()

    Dim myKey as Variant
    Dim myList As String
    myString=vbNullString
    For Each myKey in SavingFrequency

         myList = myList & "," & SavingsFrequency.Item(myKey)

    Next

    GetSavingFrequencyList=myList

End Sub

您可以使用替代字典,例如Kvp(为键值对(我自己用C#编写,是为了自己的教育)而缩写),它可以使您大大简化上面的代码。

Private SavingsFrequency as Kvp

' Later in your Module

If SavingsFrequency Is Nothing Then PopulateSavingsFrequency


Do

    Dim mySavingFrequency as String
    mySavingFrequency = InputBox("Please enter your savings frequency.")

    If SavingFrequency.HoldsValue(LCase$(mySavingFrequency)) Then Exit Do

    MsgBox ("Must be one of: " & SavingFrequency.GetValuesAsString)
       
Loop 


' Remainder of code

Public Sub PopulateSavingsFrequency()

    Set SavingsFrequency = New Kvp
    SavingsFrequency.AddByIndexFromArray split("weekly,annually")

End Sub