End Macro, if workbook can't be opened

TheIntern

New Member
Joined
Mar 15, 2011
Messages
30
Hey everyone,

I designed a Macro to copy data from another workbook that can be chosen each time.
Code:
Sub copydatafromotherworkbook()
Dim wkbk As Workbook
Dim myFileName As Variant
Set wkbk = Workbooks.Open(Filename:=myFileName)
.....
It works all right, but if the source workbook is already open or I press cancel instead of choosing a workbook an error occurs in the last line I stated here. tried to solve it like that but it doesn't work:
Code:
If myFileName = False Then
Exit Sub
End If
Any ideas? Thanks for your help,

The Intern
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I believe it's:-
Code:
If myFileName = [COLOR=red][B]"[/B][/COLOR]False[COLOR=red][B]"[/B][/COLOR] Then
Exit Sub
End If
Place a breakpoint at the Exit Sub statement and examine the contents of myFileName when you cancel out of the dialog box.
 
Upvote 0
Thanks for your help so far! Unfortunately the quotation marks don't change the situation. When I place a breakpoint on the Exit Sub the macro still runs further, so I think the
Code:
If myFileName = "False" Then
Exit Sub
End If
is still not working...is there any other approach?
 
Upvote 0
To be honest, I got this formula from the internet as well^^
When I start the macro:
Code:
Dim wkbk As Workbook
Dim myFileName As Variant
myFileName = Application.GetOpenFilename
a menu appear, where you can open an excel workbook, just as if you press the home button and than open.
 
Upvote 0
why not try

Code:
'Checks if master in use
Set wb = Workbooks.Open("your workbook")
While wb.ReadOnly
    wb.Close
    Set wb = Nothing
    Set wb = Workbooks.Open("your workbook")
    DoEvents
Wend
 
Upvote 0
Thanks a lot!!
After including
Code:
Dim x As Workbook
    On Error Resume Next
    Set x = Workbooks(wbname)
    If Err = 0 Then WorkbookIsOpen = True _
        Else WorkbookIsOpen = False
from the proposed website, it finally works!
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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