Macro when file is not found

Airborne

Board Regular
Joined
Apr 22, 2004
Messages
97
Hello,
This is the macro (also partly with your help) where I select a sheet and import it into a workbook. When cells are not the same the sheet is not imported else the sheet is imported. But I also want a message when the macrobutton is hit and the sheet doesn't exist in c:\temp and the macro is ended. I've tried "On error goto..:" and a msgbox file not found but then when the file exists I still get the msgbox.

Code:
Workbooks.Open Filename:="c:\temp\Test1.xls"
    If Sheets("Test 1").[G1] <> Workbooks("Book1").Sheets("Test2").[G5] Then

MsgBox "Wrong sheet!"
Windows("Test1.xls").Activate
    ActiveWindow.Close saveChanges:=False

Else
    Application.DisplayAlerts = False
    Sheets("test 1").Copy Before:=Workbooks("Book1"). _
    Sheets(1)
    Windows("Test1.xls").Activate
    ActiveWindow.Close saveChanges:=False
    Sheets("Test 1").Select
    Range("A4").Select

End If

End Sub

:rolleyes:
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
something like this may help

Code:
If Len(Dir("c:\temp\Test1.xls")) = 0 Then
    MsgBox "c:\temp\Test1.xls does NOT exist!"
    Exit Sub
End If

Workbooks.Open Filename:="c:\temp\Test1.xls"

If Sheets("Test 1").[G1] <> Workbooks("Book1").Sheets("Test2").[G5] Then
    MsgBox "Wrong sheet!"
    Windows("Test1.xls").Activate
    ActiveWindow.Close saveChanges:=False
Else
    Application.DisplayAlerts = False
    Sheets("test 1").Copy Before:=Workbooks("Book1"). _
    Sheets(1)
    Windows("Test1.xls").Activate
    ActiveWindow.Close saveChanges:=False
    Sheets("Test 1").Select
    Range("A4").Select
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,450
Members
449,227
Latest member
Gina V

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