这里有一些背景知识 - 我正在创建一个访问表单,允许呼叫中心的用户从多个组合框和复选框中选择选项,并根据他们的选择在文本框中生成相关的调用脚本。
我正在编写VBA代码,根据所选的选项将预先确定的文本的各个段落添加到文本框中。我已经为VBA中的每个文本段创建了一个字符串,并且我试图编写IF语句将它们组合在一起,但是最终会有大约50种不同的选项变化而且我会在在没有编写50个不同的If / ElseIf语句的情况下努力找到解决方法:|
下面是我到目前为止所提出的一个片段,其中唯一不同的是,[FullRefund]为False,取代了FullRef'字符串' OPLim'在文本框值
If [PreAuthType] = "Out Patient" _
And [FullRefund] = True _
And [ExcessLimit] > 0 _
And [Text58] < 93 _
And [OpenRef] = False _
And [TxtReq] = False _
And [ClaimsBonus] = True Then
Me.GenScript.Value = Stan & FullRef & Excess & Renewal & OpenRef & NoText & BonusTxt
ElseIf [PreAuthType] = "Out Patient" _
And [FullRefund] = False _
And [ExcessLimit] > 0 _
And [Text58] < 93 _
And [OpenRef] = False _
And [TxtReq] = False _
And [ClaimsBonus] = True Then
Me.GenScript.Value = Stan & OPLim & Excess & Renewal & OpenRef & NoText & BonusTxt
End If
您可以提供的任何帮助或指导至少让我前进将是惊人的:)
答案 0 :(得分:0)
基本上,不是为每个可能的结果编写单独的IF
语句,而是逐步构建结果。计算机程序的目的是为了消除重复,而不是增加重复。有很多方法可以解决这个问题,但这里有一个最基本的例子,因为这是(相信与否) 极简单集逻辑比较。
这已经是所需的大部分编码......
Sub BuildScript()
Dim scrOut As String 'a string to use to build the output
scrOut = stan 'standard beginning for all results, I assume?
If fullrefund Then 'if [full refund] is true then...
scrOut = scrOut & FullRef '...add this top the end of [scrOut]...
Else
scrOut = scrOut & opLim '...otherwise add this to [scrOut].
End If
If ExcessLimit > 0 Then
scrOut = scrOut & excess
Else
scrOut = scrOut & "___something else if excessLimit <= 0___"
End If
If Text58 < 93 Then
scrOut = scrOut & renewal
Else
scrOut = scrOut & "___something else if Text58 >= 93___"
End If
If openRef Then
scrOut = scrOut & openRef
Else
scrOut = scrOut & "___something of openRef=false"
End If
If TxtReq Then
scrOut = scrOut & " ___something if txtReq=true___"
Else
scrOut = scrOut & NoText
End If
If ClaimBonus Then
scrOut = scrOut & BonusTxt
Else
scrOut = scrOut & "___something else if ClaimBonus=False___"
End If
MsgBox scrOut 'display a message box with the result
'(don't worry about what to do with it until later)
End Sub
忘记上述代码,我将演示您的3选项示例:
PreAuthType(4个值),Excess(Number)和OpenRef(true / false)。如果选择了PreAuthType的选项A,B或C,那么&#39; Stan&#39;并且&#39; Excess&#39;需要返回字符串,但是如果ExcessLimit> 0,那么&#39; Excess&#39;还需要包含,如果OpenRef是True,那么&#39; OpenReferral&#39;也需要包括在内。
PreAuthType: one of: A, B, C, D
Excess: numeric value from ___ to ___
OpenRef: TRUE or FALSE only
Select Case PreAuthType
Case A, B, C
scrOut = Stan & Excess
Case D
'(does this option get anything specific?)
End Select
If Excess > 0 then
scrOut = scrOut & "The Excess is $" & Excess
end if
If OpenRef the
scrOut = scrOut & "Referral " & OpenReferral & " needs to be blah blah."
End If
请注意,在上面的IF
声明中:
If OpenRef then...
与写作完全相同:
If OpenRef = True then...
同样,这:
If Not OpenRef then...
与此相同:
If OpenRef = False then...
Using If...Then...Else Statements
(查看标准并在2个结果中进行选择。)
VBA for Beginners: VBA Case Function in a Nutshell(&#34; Select..Case&#34;)
(查看条件并在许多结果之间进行选择)
(比较&#39;关闭匹配&#39;,即:如果以A
开头,则执行此操作)