Why does this happen?

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,382
Office Version
  1. 2021
Platform
  1. Windows
I have hundreds of Excel files, but there are a handful that I use frequently. To this end, I have (In my personal.xls file) a userform, which, when loaded, displays those files as optionbutton captions (see image). I select the file required by clicking the optionbutton, then clicking the command button that says "Go". The code attached to this button is as follows:

VBA Code:
Private Sub CommandButton2_Click()
For Each but In Me.Controls
If but.name Like "Option*" Then
If but.Value = True Then
Workbooks.Open "C:\users\peter\data\xl\" & but.Caption & ".xlsm"
End
End If
End If
Next but
End Sub

The selected file opens up OK, but then Excel reverts back to showing the personal.xls window, not the window for the file that has just been opened. I don't understand why this happens. I recently ugraded
from Excel 2002 to Excel 2021, and in Excel 2002 it opened the file and stayed displaying that window.

Any suggestions as to why this might be happening, and, more importantly, how to overcome this, so that the file that has just been opened becomes the active window?
 

Attachments

  • form.jpg
    form.jpg
    80.7 KB · Views: 18

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
In any case, it all works fine with personal.xls, so I will leave well alone.
Just be aware if you use the dropdown in the record Macro dialog box and use the default Personal Workbook it will probably create a new Personal workbook in the xlsb file format and so you will have 2 Personal Workbooks.
 
Upvote 0
Nice that you found a solution, but I think you misunderstand the purpose of the Stop statement.
However, when the code is resumed, the 'End' statement is executed,
and that should stop all VB code dead in its tracks
Stop is just like a line break. When it gets there, execution halts but the procedure is still "active". If you resume, it will behave as if the stop was not there, so yes, any following lines that need to be executed will be. Such an example of a line like that would be End If, End With, End Select...
 
Upvote 0
Mark858: Thanks for the tip. I will watch out for this.
Micron: The 'End' statement after the Stop should have ceased all execution I believe.
 
Upvote 0
My apologies, I didn't look at the pic because I thought you meant End If but it was a typo. Have never used that statement and didn't eve know it existed after all these years. Live and learn I guess. Thanks.
 
  • Like
Reactions: pcc
Upvote 0

Forum statistics

Threads
1,222,625
Messages
6,167,146
Members
452,099
Latest member
Auroraaa

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