End if trouble

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I wrote the code below, but I am having trouble with end if statements at the end. I keep getting: Compile Error: Expected End With

I know this is a simple fix, but I am still feeling my way through VBA.

Sub copyandpaste()
Dim sourceFile As String
Dim firstDestFile As String
Dim secondDestFile As String
Dim thirdDestFile As String
Dim fourthDestFile As String
Dim fifthDestFile As String

sourceFile = "2011.1004.Salary Survey Template.xlsm"
firstDestFile = Workbooks(sourceFile).Path & "\byemployee.csv"
secondDestFile = Workbooks(sourceFile).Path & "\byposition.csv"
thirdDestFile = Workbooks(sourceFile).Path & "\bydepartment.csv"
fourthDestFile = Workbooks(sourceFile).Path & "\byband.csv"
fifthDestFile = Workbooks(sourceFile).Path & "\status report.xls"

If Len(Dir(firstDestFile)) = 0 Then
MsgBox firstDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
ThisWorkbook.Close savechanges:=False
Else

With Workbooks.Open(firstDestFile)

If Len(Dir(firstDestFile)) > 0 Then
.Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byemployee").Range("A1")
.Close savechanges:=False

End If
If Len(Dir(secondDestFile)) = 0 Then
MsgBox secondDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
ThisWorkbook.Close savechanges:=False
Else

With Workbooks.Open(secondDestFile)

If Len(Dir(secondDestFile)) > 0 Then
.Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byposition").Range("A1")
.Close savechanges:=False

End If
If Len(Dir(thirdDestFile)) = 0 Then
MsgBox thirdDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
ThisWorkbook.Close savechanges:=False
Else
With Workbooks.Open(thirdDestFile)
If Len(Dir(thirdDestFile)) > 0 Then
.Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("bydepartment").Range("A1")
.Close savechanges:=False

End If
If Len(Dir(fourthDestFile)) = 0 Then
MsgBox fourthDestFile & " does not exists in directory. Press OK to Continue"

Else


With Workbooks.Open(fourthDestFile)

If Len(Dir(fourthDestFile)) > 0 Then
.Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byband").Range("A1")
.Close savechanges:=False

End If
If Len(Dir(fifthDestFile)) = 0 Then
MsgBox fourthDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."

Else


With Workbooks.Open(fifthDestFile)

If Len(Dir(fifthDestFile)) > 0 Then
.Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("status report").Range("A1")
.Close savechanges:=False

End If
End With
End If


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
From first glance you have only closed one of your End Withs and hardly any of your ifs. Indentation would help you.

Code:
Sub copyandpaste()
Dim sourceFile As String
Dim firstDestFile As String
Dim secondDestFile As String
Dim thirdDestFile As String
Dim fourthDestFile As String
Dim fifthDestFile As String
sourceFile = "2011.1004.Salary Survey Template.xlsm"
firstDestFile = Workbooks(sourceFile).Path & "\byemployee.csv"
secondDestFile = Workbooks(sourceFile).Path & "\byposition.csv"
thirdDestFile = Workbooks(sourceFile).Path & "\bydepartment.csv"
fourthDestFile = Workbooks(sourceFile).Path & "\byband.csv"
fifthDestFile = Workbooks(sourceFile).Path & "\status report.xls"
If Len(Dir(firstDestFile)) = 0 Then
    MsgBox firstDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
    ThisWorkbook.Close savechanges:=False
Else
    With Workbooks.Open(firstDestFile)
        If Len(Dir(firstDestFile)) > 0 Then
           .Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byemployee").Range("A1")
           .Close savechanges:=False
        End If
    End With
    If Len(Dir(secondDestFile)) = 0 Then
        MsgBox secondDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
        ThisWorkbook.Close savechanges:=False
    Else
        With Workbooks.Open(secondDestFile)
            If Len(Dir(secondDestFile)) > 0 Then
                .Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byposition").Range("A1")
                .Close savechanges:=False
            End If
        End With
        If Len(Dir(thirdDestFile)) = 0 Then
            MsgBox thirdDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
            ThisWorkbook.Close savechanges:=False
        Else
            With Workbooks.Open(thirdDestFile)
                If Len(Dir(thirdDestFile)) > 0 Then
                    .Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("bydepartment").Range("A1")
                    .Close savechanges:=False
                End If
            End With
            If Len(Dir(fourthDestFile)) = 0 Then
                MsgBox fourthDestFile & " does not exists in directory. Press OK to Continue"
            Else
                With Workbooks.Open(fourthDestFile)
                    If Len(Dir(fourthDestFile)) > 0 Then
                        .Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("byband").Range("A1")
                        .Close savechanges:=False
                    End If
                End With
                If Len(Dir(fifthDestFile)) = 0 Then
                    MsgBox fourthDestFile & " does not exists in directory. Check to make sure the file exists or has been named correctly."
                Else
                    With Workbooks.Open(fifthDestFile)
                        If Len(Dir(fifthDestFile)) > 0 Then
                            .Worksheets(1).Cells.Copy Workbooks(sourceFile).Worksheets("status report").Range("A1")
                            .Close savechanges:=False
                        End If
                    End With
                End If
            End If
        End If
    End If
End If
End Sub

Try that
 
Upvote 0
Thank you, I did not realize that I had to write so many End if statemnets and the end.

Thanks again.
 
Upvote 0
No probs. As far as I know every If, With, or Loop needs an End. (Except single line Ifs) AFAIK.
 
Upvote 0

Forum statistics

Threads
1,202,965
Messages
6,052,836
Members
444,603
Latest member
dustinjmangum

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top