If statement

M

Mischa

Hi,



I tried to write a code that should call a other sub only when a certain
workbook is open.





The names of these workbooks are: map1.xls or; map2.xls or; map3.xls or;
map4.xls or; map5.xls.



If one of the above *.xls file is open, the sub Upgrade should be called.

If no workbook is open then sub PageSelect should be called.



The code is written in mapA.xls

But it does not work ok. You see with my code the sub PageSelect is called
five times if not one of the workbooks is open.

How should a make my code better?



Here is the code :-

---------------------------------------------------



On Error Resume Next
Windows("map1.xls").Activate
If Err = 0 Then Call Upgrade Else Call PageSelect
Windows("map2.xls").Activate
If Err = 0 Then Call Upgrade Else Call PageSelect
Windows("map3.xls").Activate
If Err = 0 Then Call Upgrade Else Call PageSelect
Windows("map4.xls").Activate
If Err = 0 Then Call Upgrade Else Call PageSelect
Windows("map5.xls").Activate
If Err = 0 Then Call Upgrade Else Call PageSelect
 
B

Bernie Deitrick

Mischa,

Dim OneOpen As Boolean
OneOpen = False

On Error Resume Next
Windows("map1.xls").Activate
If Err = 0 Then OneOpen = True
Windows("map2.xls").Activate
If Err = 0 Then OneOpen = True
Windows("map3.xls").Activate
If Err = 0 Then OneOpen = True
Windows("map4.xls").Activate
If Err = 0 Then OneOpen = True
Windows("map5.xls").Activate
If Err = 0 Then OneOpen = True
If OneOpen Then Call upgrade Else Call PageSelect

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top