【问题标题】:"Compile Error: End With without With" although there is a With block start\"Compile Error: End With without With\" 尽管有 With 块开始
【发布时间】:2022-11-29 07:48:23
【问题描述】:

我对这段代码有疑问。最终,该模块应获取社区内的呼叫次数并将其与城市平均值进行比较。

这是块:

Option Explicit

Sub CallsAverage()
    Dim neighbourhoodChoice(1 To 36) As Boolean
    Dim summaryChoice(1) As Boolean
    Dim neighbourhood As String, nCalls As Integer
    Dim dataRange As Range, message As String

    If frmOptions.ShowCallDialog(neighbourhood, nCalls) Then
        If neighbourhoodChoice(1) Then
            neighbourhood = "Allentown"
        ElseIf neighbourhoodChoice(2) Then
            neighbourhood = "Black Rock"
        ElseIf neighbourhoodChoice(3) Then
            neighbourhood = "BroadwayFillmore"
        'INSERT MORE neighbourhoodChoice
        ElseIf neighbourhoodChoice(35) Then
            neighbourhood = "West Hertel"
        Else
            neighbourhood = "West Side"
        End If

        With Worksheets(neighbourhood).Range("O4")
            nCalls = Range(.Offset(1, 0), .End("O39")).Rows
            If frmOptions.ShowCallDialog(neighbourhood, nCalls) Then
                With .Offset(0, nCalls)
                    Set dataRange = Range(.Offset(0, 1), .End("P39"))
                        
                    message = "The number of calls in" & neighbourhood & "in 2021 were" & vbCrLf
                    If nCalls > "P41" Then
                        MsgBox "The neighbourhood is less than the overall average, suggesting that it is less problematic or safer than most neighbourhoods in the city."
                    If nCalls < "P41" Then
                        MsgBox "The neighbourhood is more than the overall average, suggesting that it is more problematic or less safe than most neighbourhoods in the city."
                    If nCalls = "P41" Then
                    MsgBox "The neighbourhood is equal to the overall average, suggesting that it is no less or more problematic/safe than most neighbourhoods in the city."
                    End If
                End With <-- Issue occurs here
            End If
        End With <-- Issue occurs here too
    End If
End Sub

如果您发现我的代码有任何其他问题,非常感谢所有帮助!

【问题讨论】:

  • If nCalls &gt; "P41" Then 没有匹配的 End If 同样下面那个。 End("O39") 看起来也不对。

标签: excel vba compiler-errors


【解决方案1】:

似乎您在两个地方缺少“End If”。代码块应该是

If nCalls > "P41" Then
    MsgBox "The neighbourhood is less than the overall average, suggesting that it is less problematic or safer than most neighbourhoods in the city."
End If
If nCalls < "P41" Then
    MsgBox "The neighbourhood is more than the overall average, suggesting that it is more problematic or less safe than most neighbourhoods in the city."
End If

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2014-08-13
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多