Home > Net >  "Compile Error: End With without With" although there is a With block start
"Compile Error: End With without With" although there is a With block start

Time:11-29

I'm having an issue with this block of code. Ultimately, this module should take the number of calls within a neighbourhood and compare it to the city average.

Here is the block:

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 you spot any other issues with my code, all the help is greatly appreciated!

CodePudding user response:

Seems you are missing "End If" in two places. The code block should be

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
  • Related