I built an Excel workbook a couple of years ago to help my Dad track golf scores for a senior golf league he is in. One of the things I added was a modeless userform that should only appear when one particular sheet (named "Roster") is the ActiveSheet. I put a button at the top of the sheet to load & show the userform. (If the userform is already loaded, it will only show it.)
Everything has been working great since I made it, but today he noticed a problem. He had a second Excel file open, and when he clicked the button to show the userform, rather than it appearing on the Roster sheet, it appears on the sheet on the other Excel file that was open. I tried it on my computer and the same problem happened.
I have looked at the code related to this userform, and I can't see anything that would be causing the issue. But, I don't have a ton of experience with modeless userforms, so there's probably something simple that I'm missing. Here is all the code related to the userform.
This is the code tied to the button on the Roster sheet.
Here is the sub called by the sub above. MattData is a hidden sheet that tracks many things. There are some cases where I don't want the userform to be loaded, so that's why I do this check.
The code runs correctly. When I step through it, it goes through the entire IF block without causing an error -- it's just displaying the userform on the wrong file.
Here is the function IsFormLoaded called by the sub above.
Lastly, here is the sub ReturnFocusToExcelSheet, which is supposed to move the focus from the userform back to the Roster sheet.
Can you see any reason why the userform would be visible on the sheet in the other Excel file that is open instead of on the file that contains the code (ThisWorkbook)?
Everything has been working great since I made it, but today he noticed a problem. He had a second Excel file open, and when he clicked the button to show the userform, rather than it appearing on the Roster sheet, it appears on the sheet on the other Excel file that was open. I tried it on my computer and the same problem happened.
I have looked at the code related to this userform, and I can't see anything that would be causing the issue. But, I don't have a ton of experience with modeless userforms, so there's probably something simple that I'm missing. Here is all the code related to the userform.
This is the code tied to the button on the Roster sheet.
VBA Code:
Sub ShowMemberMenu_Button()
Call ShowMemberMenu
End Sub
Here is the sub called by the sub above. MattData is a hidden sheet that tracks many things. There are some cases where I don't want the userform to be loaded, so that's why I do this check.
VBA Code:
Sub ShowMemberMenu()
With ThisWorkbook.Sheets("MattData")
If .Range("AddOrEditMemberButtonPressed").Value = False And .Range("FileBeingImported").Value = False Then
If IsFormLoaded("RosterButtons") = False Then Load RosterButtons
ThisWorkbook.Activate
RosterButtons.Show vbModeless
ReturnFocusToExcelSheet
End If
End With
End Sub
The code runs correctly. When I step through it, it goes through the entire IF block without causing an error -- it's just displaying the userform on the wrong file.
Here is the function IsFormLoaded called by the sub above.
VBA Code:
Public Function IsFormLoaded(FrmName As String) As Boolean
Dim Frm As Object
On Error GoTo errorH
IsFormLoaded = False
For Each Frm In VBA.UserForms
If VBA.LCase$(Frm.Name) = VBA.LCase$(FrmName) Then
IsFormLoaded = True
GoTo Ending
End If
Next
errorH:
IsFormLoaded = False
Ending:
Set Frm = Nothing
End Function
Lastly, here is the sub ReturnFocusToExcelSheet, which is supposed to move the focus from the userform back to the Roster sheet.
VBA Code:
Sub ReturnFocusToExcelSheet()
On Error Resume Next
AppActivate Excel.ThisWorkbook.Application.Caption
On Error GoTo 0
End Sub
Can you see any reason why the userform would be visible on the sheet in the other Excel file that is open instead of on the file that contains the code (ThisWorkbook)?