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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If no code runs in the open wb that could cause this, I suspect it is caused by the loop in the wb code. AFAIK, normally, option buttons are single select unless you're done something that allows more than one to be selected. Your pic doesn't answer that question. If not, then after the wb is opened the code that is running is likely making the first wb active. Try adding Exit Sub after opening. Maybe like
VBA Code:
Private Sub CommandButton2_Click()
For Each but In Me.Controls
    If but.Name Like "Option*" And but.Value = True Then
        Workbooks.Open "C:\users\peter\data\xl\" & but.Caption & ".xlsm"
        Exit Sub
    End If
Next but
End Sub

Nice that you used code tags, but your code lacks indentation so not as helpful as they could be.
 
Upvote 0
Are you closing the Form? I didn't see it in your example.
VBA Code:
Unload Me
 
Upvote 0
It's bizarre. I put a 'Stop' statement into the code (see image), and at that point, the newly opened workbook is displayed, as expected. However, when the code is resumed, the 'End' statement is executed,
and that should stop all VB code dead in its tracks. But when the code completes, it is still personal.xls that is displayed as the active window, not the newly opened workbook. I have tried converting my personal.xls to personal.xlsm, (out of interest), but that made no difference to the outcome.
 

Attachments

  • image1.jpg
    image1.jpg
    96 KB · Views: 6
Upvote 0
By the way, the optionbuttons are all in the same group, so only one can be selected..
 
Upvote 0
Eureka! I have found a solution. The form that was loaded initially with all of the optionbuttons was loaded modally (not for any particular reason - that's just the default).
I changed the code from
VBA Code:
Sub showcomfilform()
ComFilForm.Show
End Sub

to

VBA Code:
Sub showcomfilform()
ComFilForm.Show False
End Sub

And it now words as intended. Strange that it worked before in Excel 2002 without the modified code. Thanks for your inputs guys.
 
Upvote 0
Solution
I have tried converting my personal.xls to personal.xlsm
Just for reference really you normally save the Personal Workbook as an xlsb file in modern windows versions

 
Upvote 0
Thanks Mark. Problem is that I have loads of macros, assigned to various customized buttons, that refer to 'personal.xls'. If I change the filename to 'personal.xlsb' then all of those macros will need to be reassigned. I can do without having to do that (Unless there's an automated way of doing that via VB)
 
Upvote 0
In VBA you can Select All and use Find and Replace. I have to do that often with older code.
 
Upvote 0
Thanks, but not as simple as that. I am referring to items on the Quick Access Toolbar, where the buttons on it have been assigned to macros in personal.xls. (In earlier versions of Excel it was something like Tools>Customise>(Right-click the button)>Assign macro). Not sure where that is now! In any case, it all works fine with personal.xls, so I will leave well alone.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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