VBA error handling problem

robw

Board Regular
Joined
Dec 18, 2002
Messages
161
Hi,
I’m having a problem with the following tract of VBA. It is intended that it should loop round until a window with a particular title (as held in string wtit) is detected. It works ok if the said window already exists before running the code, but it fails on the SECOND time round the loop if the window does not pre-exist.

I assume I’m doing something wrong in the error handling or error clearing but I cannot figure it out. Any suggestions on what I’m doing wrong, please?

Many Thanks
Rob

Code:
    For appacttry = 1 To 3
        On Error GoTo appactfail
            AppActivate wtit  ‘fails HERE on the second time round the loop  
                                              ‘with message “Run-time error 5 – Invalid procedure call or argument
        On Error GoTo 0
        GoTo appactok
appactfail:
        Err.clear  
        On Error GoTo 0
        MsgBox "failed to find window on try number  " & appacttry  
  Next appacttry
appactok:
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
To learn about error handling, have a look here http://excelmatters.com/2015/03/17/on-error-wtf/

Thanks Fluff.... The link you provided makes a lot of sense; better than the Microsoft documentation. I tried the "dirty solution" using On Error GoTo -1 and it worked. I will however also do the honourable thing and restructure my code to adopt the "clean solution". Thanks again!
Cheers
Rob
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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