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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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