On error vba ???

Alpacino

Well-known Member
Joined
Mar 16, 2011
Messages
511
I have a macro that only works if the file that it copies from is open
Eg. Code is windows("Alan1.xls").Activate
Undone want to change this line of code.

I want to say On error then msgbox " you must make sure Alan.xls is open to run macro"
Anyone help please.
Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try:
Code:
On Error Goto MyMsg
windows("Alan1.xls").Activate

MyMsg:
MsgBox "Alan1.xls must be open for macro to run, please restart"
Exit Sub
 
Upvote 0
How about this instead?
Code:
If Not IsWBOpen("Alan1.xls") Then
    Msgbox "The workbook Alan1.xls is not open. Stopping execution of code."
    Exit Sub
End If
...
 
Function IsWBOpen(strWBName As String) As Boolean
Dim wb As Workbook
 
        For Each wb In Application.Workbooks
 
            IsWbOpen = wb.Name = strWBName
       Next wb
 
End Function
 
Upvote 0
Do I put the rest of the code for the macro after the line Exit Sub for it to run normally if the file is open ??
 
Upvote 0
Norie, I think you forgot a line in the function.
Code:
[FONT=Calibri]Function IsWBOpen(strWBName As String) As Boolean[/FONT]
[FONT=Calibri]Dim wb As Workbook[/FONT]
 
[FONT=Calibri]       For Each wb In Application.Workbooks[/FONT]
 
[FONT=Calibri]           IsWBOpen = wb.Name = strWBName[/FONT]
[FONT=Calibri]           If IsWBOpen Then Exit Function[/FONT]
[FONT=Calibri]      Next wb[/FONT]
 
[FONT=Calibri]End Function[/FONT]
 
Upvote 0
Chris

Cheers, felt I'd done something wrong there but it worked when I tested it.

Mind you there werer only 2 workbooks open when I tested.:)

PS I was originally going to use this but I thought it was wrong for some reason.
Code:
    IsWBOpen = IsWbOpen Or (wb.Name = strWBName)

The Exit is probably a better idea anyway.
 
Upvote 0
Norie,

While using 'exit function' will 1/2 the processing time (on average) of the function, I like having the single exit point in the function a lot. (although to limit it to one exit point, you could use an exit for instead)

Although, considering that I wrote a function the other day that had something like 10 exit points, which for some strange reason I had to debug today, I might be a little snakebit at the moment... :)

What's your thoughts on doing something like this, instead?
Code:
Function IsWBOpen(strWBName As String) As Boolean
    Dim dummyWb                         As Workbook
    On Error GoTo OuttaHere
    Set dummyWb = Application.Workbooks(strWBName)
    Set dummyWb = Nothing
    IsWBOpen = True
    Exit Function
OuttaHere:
End Function

I'm curious because this is my general approach to finding out if an object exists/is open/etc.
 
Upvote 0
Chris

There's nothing wrong with that type of code.

I probably wouldn't use it myself, for some reason I have some sort of aversion to relying on an error which that sort of does.

Not sure if using Exit (For or Sub) would make much of a difference for a function that loops through a small collection like workbooks.

Of course if the function was more complicated, eg perhaps opening a load of workbooks, then usind Exit might have a positive impact.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,736
Members
452,940
Latest member
Lawrenceiow

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