Home > Back-end >  End With without With and End If without If compile error
End With without With and End If without If compile error

Time:11-22

I have a code that contains nested if statements, I am getting either an "End With without With" or an "End If without If" errors. I need help what I should change in my code. Here is the logic I need to create:

I have two sheets, one is called "OUTGOING ACH" and the other is called "OUTGOING WIRE". I am trying to check their contents, whatever doesn't have content should be deleted and the other one should be reformatted and called "OUTGOING". If both have contents, reformat them, combine them in one of the sheets, call it "OUTGOING", and delete the other one.

Here is a simple version of my code annotations:

'If OUTGOING ACH is empty Then
    'Delete OUTGOING ACH
    'If OUTGOING WIRE is empty
        'Delete OUTGOING WIRE
    'Else If OUTGOING WIRE isn't empty
        'Reformat OUTGOING WIRE
        'Rename OUTGOING WIRE to "OUTGOING"
    'End If
'Else If OUTGOING ACH isn't empty Then
    'Reformat OUTGOING ACH
    'Rename OUTGOING ACH to "OUTGOING"
    'If OUTGOING WIRE is empty
        'Delete OUTGOING WIRE
    'Else If OUTGOING WIRE isn't empty
        'Reformat OUTGOING WIRE
        'Copy OUTGOING WIRE to OUTGOING (formerly "OUTGOING ACH")
        'Delete OUTGOING WIRE
    'End If
'End If

I previously defined Header and Bottom as the header row and the last row with data. Here is the full code:

    With NewBatch.Sheets("OUTGOING ACH")
        Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
        Header = Application.Match("Account*", .Range("A:A"), 0)

        If Bottom = Header Then                                         'If Outgoing ACH is empty   >> If 1
            Application.DisplayAlerts = False                               'Delete it and go to Outgoing wire
            NewBatch.Sheets("OUTGOING ACH").Delete
            Application.DisplayAlerts = True
    
    End With
    
    
        
            With NewBatch.Sheets("OUTGOING WIRE")
                Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
                Header = Application.Match("Account*", .Range("A:A"), 0)
                
            If Bottom = Header Then                                     'If Outgoing wire is also empty   >> If 2
                Application.DisplayAlerts = False                           'Delete it also
                NewBatch.Sheets("OUTGOING WIRE").Delete
                Application.DisplayAlerts = True
        
            ElseIf Bottom <> Header Then                                                            'But if Outgoing wire is not empty   >> Else 2
                .Activate                                               'Reformat OUTGOING WIRE
                .Columns("B:D").Delete Shift:=xlToLeft
                .Columns("C:Z").Delete Shift:=xlToLeft
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("E:E").Cut
                .Columns("D:D").Insert Shift:=xlToRight
                .Range("A" & Header).Select
                Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
                ActiveCell.Offset(0, 1).Select
                Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
                ActiveCell.Offset(0, 2).Select
                ActiveCell.FormulaR1C1 = "Third Party"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "CCY"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Batch ID"
                .Range(("E" & Header   1) & ":" & "E" & Bottom).Copy
                .Range(("A" & Header   1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                .Range(("B" & Header   1) & ":" & "B" & Bottom).Value = "FEDW"
                .Range(("C" & Header   1) & ":" & "C" & Bottom).Value = Date
                .Range(("F" & Header   1) & ":" & "F" & Bottom).Value = "USD"
                .Range(("G" & Header   1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
                .Range(("G" & Header   1) & ":" & "G" & Bottom).NumberFormat = "#"
                .Range("H" & Header).Value = "-1"
                .Range("H" & Header).Copy
                .Range(("D" & Header   1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
                SkipBlanks:=False, Transpose:=False
                .Range("H" & Header).ClearContents
                .Rows("1:" & Header - 1).Delete Shift:=xlUp
                .Cells.Select
                    With Selection.Interior
                        .Pattern = xlNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    With Selection.Font
                        .Name = "Calibri"
                        .Size = 11
                        .Strikethrough = False
                        .Superscript = False
                        .Subscript = False
                        .OutlineFont = False
                        .Shadow = False
                        .Underline = xlUnderlineStyleNone
                        .ThemeColor = xlThemeColorLight1
                        .TintAndShade = 0
                        .ThemeFont = xlThemeFontMinor
                        .Bold = False
                    End With
                    With Selection.Borders
                        .LineStyle = xlNone
                    End With
                    With Selection
                        .WrapText = False
                        .EntireColumn.AutoFit
                        .EntireRow.AutoFit
                    End With
                .Columns("D:D").NumberFormat = "0.00"
                .Range("A1").Select
                .Name = ("OUTGOING")
            End If                                                      '>> End of If 2 (ACH is empty and whether or not Wire is empty)
            End With
                                                                    'Now, if Outgoing ACH wasn't empty >> Else If 1
                                                                        'First reformat Outgoing ACH
            With NewBatch.Sheets("OUTGOING ACH")
                Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
                Header = Application.Match("Account*", .Range("A:A"), 0)
        
        ElseIf Bottom <> Header Then
                .Activate
                .Columns("B:D").Delete Shift:=xlToLeft
                .Columns("C:Z").Delete Shift:=xlToLeft
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("E:E").Cut
                .Columns("D:D").Insert Shift:=xlToRight
                .Range("A" & Header).Select
                Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
                ActiveCell.Offset(0, 1).Select
                Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
                ActiveCell.Offset(0, 2).Select
                ActiveCell.FormulaR1C1 = "Third Party"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "CCY"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Batch ID"
                .Range(("E" & Header   1) & ":" & "E" & Bottom).Copy
                .Range(("A" & Header   1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                .Range(("B" & Header   1) & ":" & "B" & Bottom).Value = "CCD"
                .Range(("C" & Header   1) & ":" & "C" & Bottom).Value = Date
                .Range(("F" & Header   1) & ":" & "F" & Bottom).Value = "USD"
                .Range(("G" & Header   1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
                .Range(("G" & Header   1) & ":" & "G" & Bottom).NumberFormat = "#"
                .Range("H" & Header).Value = "-1"
                .Range("H" & Header).Copy
                .Range(("D" & Header   1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
                SkipBlanks:=False, Transpose:=False
                .Range("H" & Header).ClearContents
                .Rows("1:" & Header - 1).Delete Shift:=xlUp
                .Cells.Select
                    With Selection.Interior
                        .Pattern = xlNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    With Selection.Font
                        .Name = "Calibri"
                        .Size = 11
                        .Strikethrough = False
                        .Superscript = False
                        .Subscript = False
                        .OutlineFont = False
                        .Shadow = False
                        .Underline = xlUnderlineStyleNone
                        .ThemeColor = xlThemeColorLight1
                        .TintAndShade = 0
                        .ThemeFont = xlThemeFontMinor
                        .Bold = False
                    End With
                    With Selection.Borders
                        .LineStyle = xlNone
                    End With
                    With Selection
                        .WrapText = False
                        .EntireColumn.AutoFit
                        .EntireRow.AutoFit
                    End With
                .Columns("D:D").NumberFormat = "0.00"
                .Range("A1").Select
                .Name = ("OUTGOING")
            End With
            
        With NewBatch.Sheets("OUTGOING WIRE")                           'Then check if Ougoing Wire is empty
        
            Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
            Header = Application.Match("Account*", .Range("A:A"), 0)
                
                If Bottom = Header Then                                 'If Outgoing Wire is empty   >> If 3
                    Application.DisplayAlerts = False                       'Delete Outgoing Wire
                    NewBatch.Sheets("OUTGOING WIRE").Delete
                    Application.DisplayAlerts = True
        
                ElseIf Bottom <> Header Then                                                        'If Outgoing Wire isn't empty (both aren't empty)   >> Else if 3
                .Activate                                               'Reformat outgoing wire
                .Columns("B:D").Delete Shift:=xlToLeft
                .Columns("C:Z").Delete Shift:=xlToLeft
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("A:A").Insert Shift:=xlToRight
                .Columns("E:E").Cut
                .Columns("D:D").Insert Shift:=xlToRight
                .Range("A" & Header).Select
                .Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
                ActiveCell.Offset(0, 1).Select
                Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
                ActiveCell.Offset(0, 2).Select
                ActiveCell.FormulaR1C1 = "Third Party"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "CCY"
                ActiveCell.Offset(0, 1).Select
                ActiveCell.FormulaR1C1 = "Batch ID"
                .Range(("E" & Header   1) & ":" & "E" & Bottom).Copy
                .Range(("A" & Header   1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                .Range(("B" & Header   1) & ":" & "B" & Bottom).Value = "FEDW"
                .Range(("C" & Header   1) & ":" & "C" & Bottom).Value = Date
                .Range(("F" & Header   1) & ":" & "F" & Bottom).Value = "USD"
                .Range(("G" & Header   1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
                .Range(("G" & Header   1) & ":" & "G" & Bottom).NumberFormat = "#"
                .Range("H" & Header).Value = "-1"
                .Range("H" & Header).Copy
                .Range(("D" & Header   1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
                SkipBlanks:=False, Transpose:=False
                .Range("H" & Header).ClearContents
                .Rows("1:" & Header - 1).Delete Shift:=xlUp
                .Cells.Select
                    With Selection.Interior
                        .Pattern = xlNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    With Selection.Font
                        .Name = "Calibri"
                        .Size = 11
                        .Strikethrough = False
                        .Superscript = False
                        .Subscript = False
                        .OutlineFont = False
                        .Shadow = False
                        .Underline = xlUnderlineStyleNone
                        .ThemeColor = xlThemeColorLight1
                        .TintAndShade = 0
                        .ThemeFont = xlThemeFontMinor
                        .Bold = False
                    End With
                    With Selection.Borders
                        .LineStyle = xlNone
                    End With
                    With Selection.Borders
                        .WrapText = False
                        .EntireColumn.AutoFit
                        .EntireRow.AutoFit
                    End With
            .Columns("D:D").NumberFormat = "0.00"
            Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
            .Rows("2" & ":" & Bottom).Copy
        End With
        
            With NewBatch.Sheets("OUTGOING")
                Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
                .Rows(Bottom   1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                .Range("A1").Select
            End With
            Application.DisplayAlerts = False
            Sheets("OUTGOING WIRE").Delete
            Application.DisplayAlerts = True

                End If                                                  'End of If 3 (ACH isn't empty and whether or not wire is empty)
        End If                                                          'End of If 1 (The whole test for ACH and Outgoing Wire)

I tried to reposition End If and End With but that didn't work.

Thanks!

CodePudding user response:

As others have pointed out there are many errors in your code, but to name a few:

With NewBatch.Sheets("OUTGOING ACH")
    Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
    Header = Application.Match("Account*", .Range("A:A"), 0)

    If Bottom = Header Then                                         'If Outgoing ACH is empty   >> If 1
        Application.DisplayAlerts = False                               'Delete it and go to Outgoing wire
        NewBatch.Sheets("OUTGOING ACH").Delete
        Application.DisplayAlerts = True
    End If '<= this is the closing `End If` for 'If Bottom = Header Then'           
End With

Further, the section staring with:

With NewBatch.Sheets("OUTGOING ACH")
   Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
   Header = Application.Match("Account*", .Range("A:A"), 0)
   
   If (something = true) Then '<= Missing `If` initialization in your code
       'do something
   ElseIf Bottom <> Header Then

       'whole bunch of code ending with last few lines as per below

       .Columns("D:D").NumberFormat = "0.00"
       .Range("A1").Select
       .Name = ("OUTGOING")
   End If '<= Missing `End If` statement to close the `If (something = true) Then' statement
End With

Subsequently section starting with:

With NewBatch.Sheets("OUTGOING WIRE")                           'Then check if Ougoing Wire is empty
        
    Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
    Header = Application.Match("Account*", .Range("A:A"), 0)
                
    If Bottom = Header Then 'If Outgoing Wire is empty   >> If 3
        Application.DisplayAlerts = False 'Delete Outgoing Wire
        NewBatch.Sheets("OUTGOING WIRE").Delete
        Application.DisplayAlerts = True
        
    ElseIf Bottom <> Header Then

        'whole bunch of code ending with last few lines as per below

        .Columns("D:D").NumberFormat = "0.00"
        Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
        .Rows("2" & ":" & Bottom).Copy
    End If '<= Missing 'End If' to close the 'If Bottom = Header Then' statement
End With

Finally:

    With NewBatch.Sheets("OUTGOING")
        Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
        .Rows(Bottom   1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                .Range("A1").Select
    End With
    Application.DisplayAlerts = False
    Sheets("OUTGOING WIRE").Delete
    Application.DisplayAlerts = True

    End If 'End of If 3 (ACH isn't empty and whether or not wire is empty)
End If '<= These last two 'End If' statements do not have an opening statement? i.e. a corresponding 'If (something = true) Then'

Remember to correctly encapsulate your If statements as they should be within the With statement

With <something>

    If (something = true) Then
        'do something
    ElseIf (somethingElse = true) Then
        'do something else
    Else
        'do fallback
    End If

End With
  • Related