pops up message when run the userform if specific sheet is hidden

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hello

I have many sheets are hidden except sheet MAIN to run userform from of it . when I run the userform and the sheet DATA is hidden , then should pops up messge " threre is missed data" and close the userform , and if the sheet DATA is not hidden when run the userform should keep showing .
and if try up to 3 times and three times shows the message then should pops up message" sorry run out your tries" and close the file

thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
try this in userform initialize event
VBA Code:
Private Sub UserForm_Initialize()
If FormShowCount = 3 Then
     MsgBox "A message here"
     Exit Sub
End If

If Sheets("DATA").Visible = False Then
     MsgBox "Your message here"
     Unload Me
End If

FormShowCount = FormShowCount + 1

End Sub
You would need a module level variable:
Public FormShowCount As Integer

in a standard module.
 
Upvote 0
thanks . I don't see the line to close the file after three times and gives error .
about this
Public FormShowCount As Integer
are you sure about put in standard module?
 
Upvote 0
thanks . I don't see the line to close the file after three times and gives error .
about this
If FormShowCount = 3 Then
If you want after 3 then change to = 4 OR >= 3
are you sure about put in standard module?
Put what? Public FormShowCount As Integer? Then yes.
and gives error
That tells me nothing. That code raises an error? Then what is the message and which line gets highlighted?
 
Upvote 0
I use F8 and shows error as Pic in the end code END SUB
1.PNG
 
Upvote 0
I'll bet it's because the form closes before the opening code completes. Will figure out if that's the case and how to fix.
 
Upvote 0
That is the problem - the event that opens the form complains because the form no longer exists. I presume you're using a sheet activeX button to open userform. This would be one way to eliminate the error message that results:
VBA Code:
Private Sub CommandButton1_Click()

On Error GoTo errHandler
UserForm.Show
Exit Sub

errHandler:
If Err.Number = 91 Then
     Resume Next
Else
     MsgBox "Error " & Err.Number & ": " & Err.Description
End If

End Sub
 
Upvote 0
actually I no know how your solution works
may you see my file , guide me what's my mistake , please?
LABLE.xlsm
 
Upvote 0
You named your userform as userform1. Also, you put the public variable in two times. The public variable in the userform code should not be there. I changed =3 to >=2 because FormShowCount begins at zero, so 3 tries is 0,1,2. I changed the messages to make it easier for me to see what was happening. Try the following:
VBA Code:
Private Sub CommandButton1_Click()

On Error GoTo errHandler
UserForm1.Show '<<changed to your userform name
Exit Sub

errHandler:
If Err.Number = 91 Then
     Resume Next
Else
     MsgBox "Error " & Err.Number & ": " & Err.Description
End If

End Sub

Private Sub UserForm_Initialize() '<<do not change to userform1 here
If FormShowCount >= 2 Then
     MsgBox "You have reached the maximum tries allowed."
     Unload Me
     Exit Sub
End If

If Sheets("DATA").Visible = False Then
     MsgBox "Cannot proceed if Data sheet is hidden"
     Unload Me
End If

FormShowCount = FormShowCount + 1

End Sub
 
Upvote 0
thanks
as I said
and if try up to 3 times and three times shows the message then should pops up message" sorry run out your tries" and close the file
should close the file after three times when run the userform
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

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