在VBA中组织嵌套条件语句的更好方法是什么?

时间:2018-12-28 01:54:02

标签: vba ms-word

我正在尝试寻找一种更简单的方法来使程序产生具有多个结果的句子。我还想在语句之间包括一个“逗号”或“和”,这取决于是否只有那些2或它是序列中的最后一个。朝正确方向的观点会有所帮助。

一个完整的肯定句子是“观看视频后,个人保持顺从,有良好的判断力,负责任,表现出良好的客户服务并表现出安全的作法”

完整的句子:“观看视频后,该人具有良好的判断力,表现出良好的客户服务并显示了安全的做法,很不幸,该人不遵守规定并且不负责任。”

我一直在研究使用电源套件,但似乎需要更多工作。

Function VidSent2(HeShe, Vcomp, Vjudg, Vresp, Vcust, Vsafe)
'HeShe is just to see if it should use "he" or "she"

Pstmt1 = "": Nstmt1 = ""
Pstmt2 = "": Nstmt2 = "": a1 = "": c1 = ""
Pstmt3 = "": Nstmt3 = "": a2 = "": c2 = ""
Pstmt4 = "": Nstmt4 = "": a3 = "": c3 = ""
Pstmt5 = "": Nstmt5 = "": a4 = "": c4 = "" 

A = 0

If Vcomp = True Then
    Pstmt1 = "was compliant" ' what comes after this? , or and?
    A = A + 1
Else
    Nstmt1 = "was not compliant"
End If

If Vjudg = True Then
    Pstmt2 = "had good judgment"
    A = A + 1
Else
    Nstmt2 = "had bad judment"
End If

If Vresp = True Then
    Pstmt2 = "was responsible"
    A = A + 1
Else
    Nstmt2 = "was not responsible"
End If

If Vcust = True Then
    Pstmt2 = "showed good customer service"
    A = A + 1
Else
    Nstmt2 = "showed bad customer service"
End If

If Vsafe = True Then
    Pstmt2 = "showed safe practices."
    A = A + 1
Else
    Nstmt2 = "did not show safe practices."
End If

End If

'might need to return a text value
bmRange.Text = Pstmt1 & c1 & a1 & Pstmt2 & c2 & a2 & Pstmt3 & c3 & a3 &     Pstmt4 & c4 & a4 & Pstmt5 & c5 & a5 & "unfortunately " & HeShe & _
Nstmt1 & Nstmt2 & Nstmt3 & Nstmt4 & Nstmt5

'after viewing the video i observed the individual "
'had good judgment ", unfortunately "heshe"...
'was compliant " and " had good judgment
'was compliant ", " had good judgment " and " was responsible

 end function

该句子有32种可能的结果,具体取决于它们是位于“不幸的是”左侧还是右侧。 (已经解决了它们全部位于一侧或另一侧都位于的情况。)我正在尝试在语句后添加逗号还是“ and”添加它。

我发布的代码也是“进行中的工作”,因为我想看看是否有更好的方法来代替暴力破解,这似乎是我的前进方向。它尚未包含逗号或“ and”的代码。

编辑:我最终选择做的是接受MacroPod的回答并对其进行一些调整(我知道我可以清理它,并且将来会这样做。它可以做我大致需要做的事情。)谢谢每个帮助过的人。

Function VidSent2(Comp As Boolean, Judg As Boolean, Resp As Boolean, Cust As Boolean, Safe As Boolean)
Dim ArrPos(), ArrNeg(), StrOut As String
ReDim Preserve ArrPos(0): ReDim Preserve ArrNeg(0): StrOut = ""

If Comp = True Then
    If UBound(ArrPos) >= 0 Then
        ArrPos(UBound(ArrPos)) = "was compliant"
        ReDim Preserve ArrPos(0 To UBound(ArrPos) + 1)
    End If
Else
    If UBound(ArrNeg) >= 0 Then
        ArrNeg(UBound(ArrNeg)) = "was not compliant"
        ReDim Preserve ArrNeg(0 To UBound(ArrNeg) + 1)
    End If
End If

If Judg = True Then
    If UBound(ArrPos) >= 0 Then
        ArrPos(UBound(ArrPos)) = "had good judgment"
        ReDim Preserve ArrPos(0 To UBound(ArrPos) + 1)
    End If
Else
    If UBound(ArrNeg) >= 0 Then
        ArrNeg(UBound(ArrNeg)) = "had bad judgment"
        ReDim Preserve ArrNeg(0 To UBound(ArrNeg) + 1)
    End If
End If

If Resp = True Then
    If UBound(ArrPos) >= 0 Then
        ArrPos(UBound(ArrPos)) = "was responsible"
        ReDim Preserve ArrPos(0 To UBound(ArrPos) + 1)
    End If
Else
    If UBound(ArrNeg) >= 0 Then
        ArrNeg(UBound(ArrNeg)) = "was not responsible"
        ReDim Preserve ArrNeg(0 To UBound(ArrNeg) + 1)
    End If
End If

If Cust = True Then
    If UBound(ArrPos) >= 0 Then
        ArrPos(UBound(ArrPos)) = "showed good customer service"
        ReDim Preserve ArrPos(0 To UBound(ArrPos) + 1)
    End If
Else
    If UBound(ArrNeg) >= 0 Then
        ArrNeg(UBound(ArrNeg)) = "showed bad customer service"
        ReDim Preserve ArrNeg(0 To UBound(ArrNeg) + 1)
    End If
End If

If Safe = True Then
    If UBound(ArrPos) >= 0 Then
        ArrPos(UBound(ArrPos)) = "showed safe practices"
        ReDim Preserve ArrPos(0 To UBound(ArrPos) + 1)
    End If
Else
    If UBound(ArrNeg) >= 0 Then
        ArrNeg(UBound(ArrNeg)) = "did not show safe practices"
        ReDim Preserve ArrNeg(0 To UBound(ArrNeg) + 1)
    End If
End If
ReDim Preserve ArrPos(0 To UBound(ArrPos) - 1)
ReDim Preserve ArrNeg(0 To UBound(ArrNeg) - 1)
If UBound(ArrPos) > 0 Then ArrPos(UBound(ArrPos)) = "and " & ArrPos(UBound (ArrPos))
If UBound(ArrNeg) > 0 Then ArrNeg(UBound(ArrNeg)) = "and " & ArrNeg(UBoun(ArrNeg))

If ArrPos(UBound(ArrPos)) <> "" Then StrOut = "After viewing the video the individual " & Join(ArrPos(), ", ") & "."

If ArrNeg(UBound(ArrNeg)) <> "" Then StrOut = StrOut & " Unfortunately, the individual still " & Join(ArrNeg(), ", ") & "."

VidSent2 = StrOut
End Function

2 个答案:

答案 0 :(得分:-1)

我认为ElseIf语句是最简单的方法。这是一个例子。

If Range(“A5”) <= 50 Then
    Range (“A6”).Value = “Failing Grade”
Else If Range (“A5”) <= 80 Then 
    Range(“A6”).Value = “Passing Grade”
Else 
    Range(“A6”).Value = “Outstanding Grade”
End If

答案 1 :(得分:-1)

尝试类似的东西:

Dim ArrPos(), ArrNeg(), StrOut As String

Function VidSent2(Comp As Boolean, Judg As Boolean, Resp As Boolean, Cust As Boolean, Safe As Boolean)
ReDim ArrPos(0): ReDim ArrNeg(0): StrOut = ""
If Comp = True Then Call Pos("was compliant") Else Call Neg("was not compliant")
If Judg = True Then Call Pos("had good judgment") Else Call Neg("had bad judment")
If Resp = True Then Call Pos("was responsible") Else Call Neg("was not responsible")
If Cust = True Then Call Pos("showed good customer service") Else Call Neg("showed bad customer service")
If Safe = True Then Call Pos("showed safe practices") Else Call Neg("did not show safe practices")

If UBound(ArrPos) > 0 Then ArrPos(UBound(ArrPos)) = "and " & ArrPos(UBound(ArrPos))
If UBound(ArrNeg) > 0 Then ArrNeg(UBound(ArrNeg)) = "and " & ArrNeg(UBound(ArrNeg))

If ArrPos(UBound(ArrPos)) <> "" Then StrOut = "After viewing the video the individual " & Join(ArrPos(), ", ") & "."

If ArrNeg(UBound(ArrNeg)) <> "" Then
  If StrOut = "" Then
    StrOut = "Regrettably, after viewing the video the individual still " & Join(ArrNeg(), ", ") & "."
  Else
    StrOut = StrOut & " Unfortunately, the individual still " & Join(ArrNeg(), ", ") & "."
  End If
End If

VidSent2 = StrOut
End Function

Sub Pos(StrTxt As String)
If ArrPos(UBound(ArrPos)) <> "" Then ReDim Preserve ArrPos(UBound(ArrPos) + 1)
ArrPos(UBound(ArrPos)) = StrTxt
End Sub

Sub Neg(StrTxt As String)
If ArrNeg(UBound(ArrNeg)) <> "" Then ReDim Preserve ArrNeg(UBound(ArrNeg) + 1)
ArrNeg(UBound(ArrNeg)) = StrTxt
End Sub

您将使用以下代码调用该代码:

Sub Test()
bmRange.Text = VidSent2(True, False, True, True, False)
End Sub
相关问题