【问题标题】:If ElseIf outputs after responding to askMsgIf ElseIf 响应 askMsg 后输出
【发布时间】:2017-08-01 07:24:46
【问题描述】:

上周我的上一个问题得到了很好的回答,我回来寻求更多指导。

我们有一个 vba 宏,它遍历 Word 文档中的修订次数,然后根据字数为我们提供难度分数。最近,我们开始使用两个不同的过程,需要两个不同的等级,但它们基于相同的算法(修订数/字数)。本质上,我正在尝试制作一个 If ElseIf ,它将根据使用的过程返回分数。前端的计算都是一样的,所以只需要实现正确的定义即可。

当我添加askMsg问题时,原始过程仍然完好无损;但是,当我尝试运行第二个进程时,返回值为 0(应该是 1-10 的值)。我将新流程作为 If 还是 ElseIf 都没有关系;原始代码的工作原理相同,而新代码尽管它们几乎相同,但它们的工作原理相同。我已经包含了完整的代码,并用 'XX 指明了关注的行。

Sub Grade()

Dim oDoc As Document
Dim nWords As Long
Dim commentCount As Long
Dim revisionCount As Long
Dim totalRev As Long
'Dim hasChanges As Boolean
'Dim oRevision As Revision
'Dim oComment As comment
Dim rDensity As Variant
Dim mGrade As Long

Set oDoc = ActiveDocument
' update and count number of words
nWords = oDoc.Range.ComputeStatistics(wdStatisticWords)

' The following line was added to handle an accidental macro-button press on an empty
' document. It executes only if nWords is 0 to avoid the divide-by-zero error; it does not handle an
' error in getting the word count.
If nWords = 0 Then mGrade = 10: GoTo ExitManuscriptGrade

'check to see if the document has changes,
revisionCount = oDoc.Revisions.count ' get the revision count
commentCount = oDoc.Comments.count ' get the number of comments

' all the following scoring code was left exactly the same
totalRev = commentCount + revisionCount

'calculate density of revisions
rDensity = totalRev / nWords

'XX Here is where the issues start XX

askMsg = "Is this a Top Tier Sub?"
msgResult = MsgBox(askMsg, vbYesNo)
        Select Case msgResult
            Case vbYes
            TopTier = True
            Case vbNo
            TopTier = False
        End Select


If TopTier = True Then
'Assign manuscript grade
    If rDensity > 0.4 Then
        mGrade = 1
    ElseIf rDensity <= 0.4 And rDensity > 0.37 Then
        mGrade = 2
    ElseIf rDensity <= 0.37 And rDensity > 0.34 Then
        mGrade = 3
    ElseIf rDensity <= 0.34 And rDensity > 0.3 Then
        mGrade = 4
    ElseIf rDensity <= 0.3 And rDensity > 0.26 Then
        mGrade = 5
    ElseIf rDensity <= 0.22 And rDensity > 0.26 Then
        mGrade = 6
    ElseIf rDensity <= 0.18 And rDensity > 0.22 Then
        mGrade = 7
    ElseIf rDensity <= 0.16 And rDensity > 0.18 Then
        mGrade = 8
    ElseIf rDensity <= 0.13 And rDensity > 0.16 Then
        mGrade = 9
    ElseIf rDensity <= 0.13 Then
        mGrade = 10
    End If

'Message box output for testing
    MsgBox (mGrade) ' XX mGrade is always set to zero XX
Exit Sub

ElseIf TopTier = False Then
'XX This is where the code originally went prior to adding in askMsg XX
'Assign manuscript grade
    If rDensity > 0.31 Then
        mGrade = 1
    ElseIf rDensity <= 0.31 And rDensity > 0.27 Then
        mGrade = 2
    ElseIf rDensity <= 0.27 And rDensity > 0.24 Then
        mGrade = 3
    ElseIf rDensity <= 0.24 And rDensity > 0.2 Then
        mGrade = 4
    ElseIf rDensity <= 0.2 And rDensity > 0.18 Then
        mGrade = 5
    ElseIf rDensity <= 0.18 And rDensity > 0.16 Then
        mGrade = 6
    ElseIf rDensity <= 0.16 And rDensity > 0.13 Then
        mGrade = 7
    ElseIf rDensity <= 0.13 And rDensity > 0.11 Then
        mGrade = 8
    ElseIf rDensity <= 0.11 And rDensity > 0.09 Then
        mGrade = 9
    ElseIf rDensity <= 0.09 Then
        mGrade = 10
    End If

'Message box output for testing
    MsgBox (mGrade) ' XX This outputs the mGrade correctly XX
    End If
Exit Sub

' Execution jumps to this label if there are no words in the document
ExitManuscriptGrade:

End Sub

我不确定我是否遗漏了一个小命令或需要其他一些 Else 语句,但我们将不胜感激任何帮助!

【问题讨论】:

  • If TopTier = True 上放一个断点,然后检查此时rDensity 的值是多少?然后使用 F8 单步执行代码以查看发生了什么(以及原因)。

标签: vba if-statement ms-word


【解决方案1】:

您有一些条件永远不会为真,请检查您的逻辑,例如:If rDensity &lt;= 0.22 And rDensity &gt; 0.26 Then。在这种情况下,rDensity 不能是 LTE .22 和 GT .26。接下来的 3 个条件有相同的故障。

看看这个,它将向您展示一些基本的调试技术,您可以使用这些技术来单步调试您的代码并在未来进行故障排除:

http://www.cpearson.com/excel/DebuggingVBA.aspx

我认为这将解决它。请注意,我已将您的 If/ElseIf 语句重组为 Case 语句,并使用布尔表达式来获得 TopTier 值(而不是在不需要的地方使用 Case 语句)。

Sub Grade()

Dim oDoc As Document
Dim nWords As Long
Dim commentCount As Long
Dim revisionCount As Long
Dim totalRev As Long
'Dim hasChanges As Boolean
'Dim oRevision As Revision
'Dim oComment As comment
Dim rDensity As Double
Dim mGrade As Long

Set oDoc = ActiveDocument
' update and count number of words
nWords = oDoc.Range.ComputeStatistics(wdStatisticWords)

' The following line was added to handle an accidental macro-button press on an empty
' document. It executes only if nWords is 0 to avoid the divide-by-zero error; it does not handle an
' error in getting the word count.
If nWords = 0 Then GoTo ExitManuscriptGrade

' check to see if the document has changes,
revisionCount = oDoc.Revisions.count ' get the revision count
commentCount = oDoc.Comments.count ' get the number of comments

' all the following scoring code was left exactly the same
totalRev = commentCount + revisionCount
' calculate density of revisions
rDensity = totalRev / nWords

askMsg = "Is this a Top Tier Sub?"
msgResult = MsgBox(askMsg, vbYesNo)
TopTier = msgResult = vbYesNo

If TopTier Then
'Assign manuscript grade
    Select Case True
        Case rDensity > 0.4
            mGrade = 1
        Case 0.37 < rDensity <= 0.4 
            mGrade = 2
        Case 0.34 < rDensity <= 0.37
            mGrade = 3
        Case 0.3 < rDensity <= 0.34 
            mGrade = 4
        Case 0.26 < rDensity <= 0.3 
            mGrade = 5
        Case 0.22 < rDensity <= 0.26 
            mGrade = 6
        Case 0.18 < rDensity <= 0.22 
            mGrade = 7
        Case 0.16 < rDensity <= 0.18 
            mGrade = 8
        Case 0.13 < rDensity <= 0.16 
            mGrade = 9
        Case rDensity <= 0.13
            mGrade = 10
    End If
    'Message box output for testing
    MsgBox (mGrade) ' XX mGrade is always set to zero XX
Else 
'XX This is where the code originally went prior to adding in askMsg XX
'Assign manuscript grade
    Select Case True
        Case rDensity > 0.31
            mGrade = 1
        Case 0.27 < rDensity <= 0.31
            mGrade = 2
        Case .24 < rDensity <= .27
            mGrade = 3
        Case 0.2  < rDensity <= .24
            mGrade = 4
        Case 0.18 < rDensity <= 0.2
            mGrade = 5
        Case 0.16 < rDensity <= 0.18
            mGrade = 6
        Case 0.13 < rDensity <= 0.16
            mGrade = 7
        Case 0.11 < rDensity <= 0.13
            mGrade = 8
        Case 0.09 < rDensity <= 0.11
            mGrade = 9
        Case rDensity <= 0.09
            mGrade = 10
    End Select
    'Message box output for testing
    MsgBox (mGrade) ' XX This outputs the mGrade correctly XX
End If
' Execution jumps to this label if there are no words in the document
ExitManuscriptGrade:

End Sub

【讨论】:

  • 你不能使用 'Select Case rDensity' 然后 'Case > 0.4' , 'Case 0.31 To 0.4' 等等?
  • 感谢您的回复 - 事实证明错误的逻辑(我在其中有向后的参数)是导致代码出错的原因。一旦我修复了逻辑,一切就开始工作了。
猜你喜欢
  • 2010-10-12
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
相关资源
最近更新 更多