Help with UserForm Command Buttons

furstukin

Board Regular
Joined
Apr 22, 2011
Messages
71
I have a spreadsheet that has worked fine for me sincei made it a few months ago. IT calls up a series of user forms and asks for info then places it all where it needs to be.

A co-worker aksed me if I could make it work for her as well and I figured why not it should be as simple as reproducing the code and changing a few pieces of text so it asks the user for the correct information.

Trouble is when I added the relevant pieces of code and the imported the same UserForms no matter what command button I hit it always continues following hte macro script and ignores what the comand button tells it to do.

Here is the code for the first user form.

Code:
Option Explicit
Public Run As Boolean
Public Stopped As Boolean
 
Private Sub CommandButton1_Click()
    Run = True
    Unload Me
End Sub
 
Private Sub CommandButton2_Click()
    Stopped = True
    Unload Me
End Sub
 
Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Would you like to run the Shrinkage Tool Wizard?"
End Sub

as you can see it is simple and strait forward

And here is the code that calls the UserForm and tells it what to do.

Code:
Public Week As String
Public Run As Boolean
Public RunSSCOE As Boolean
Public Stopped As Boolean
Public STOPSSCOE As Boolean
Public SSCOE As Boolean
Public ALLDONE As Boolean
Option Explicit
 
Sub Grab_Info()
 
Week = Range("A1").Value
 
Stopped = False
UserFormShrink.Show
If Stopped Then GoTo StopNow
 
Application.DisplayAlerts = False
 
STOPSSCOE = False
UserFormSSCOE.Show
If STOPSSCOE Then GoTo StopNow
If SSCOE Then GoTo NoMore
If RunSSCOE Then GoTo Group1
 
Group1: Sheets("temp").Visible = True      'Unhide temp sheet add data, copy average shrinkage and rehide temp"
Sheets("temp").Activate
Cells.Select
ActiveSheet.Paste
Range("A300:AC303").Select
Selection.Copy
 
Sheets("site averages").Visible = True
Sheets("site averages").Activate
Range("b1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("temp").Visible = False
Sheets("site averages").Visible = False
 
NoMore: MsgBox "No line groups remain. Wizard complete!"
 
StopNow: Sheets("Attrition").Activate
Range("A1").Select
 
Application.DisplayAlerts = True
 
Exit Sub
End Sub

As you might be able to tell my co-worker only needs this to calculate the shrinkage of one group where as I was using it with 6 groups and had a seperate userform and similar code for each group. And it always worked perfectly allowing me to skip a group cancel the wizard or continue as normal based on the button I would hit. But now if I hit Cancel or CommandButton2 which = Stopped it shows I have hit CommandButton2 unloads the user form and then acts as if Stopped still = False continueing on with the code rather then going to the StopNow line I indicated it should. Am I missing something simple here? Cause I have tried anything and everything I can think of with no luck all the while the workbook I originally wrote this in everything works just fine...:(
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I have a spreadsheet that has worked fine for me sincei made it a few months ago. IT calls up a series of user forms and asks for info then places it all where it needs to be.

A co-worker aksed me if I could make it work for her as well and I figured why not it should be as simple as reproducing the code and changing a few pieces of text so it asks the user for the correct information.

Trouble is when I added the relevant pieces of code and the imported the same UserForms no matter what command button I hit it always continues following hte macro script and ignores what the comand button tells it to do.

Here is the code for the first user form.

Code:
Option Explicit
Public Run As Boolean
Public Stopped As Boolean
 
Private Sub CommandButton1_Click()
    Run = True
    Unload Me
End Sub
 
Private Sub CommandButton2_Click()
    Stopped = True
    Unload Me
End Sub
 
Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Would you like to run the Shrinkage Tool Wizard?"
End Sub

as you can see it is simple and strait forward

And here is the code that calls the UserForm and tells it what to do.

Code:
Public Week As String
Public Run As Boolean
Public RunSSCOE As Boolean
Public Stopped As Boolean
Public STOPSSCOE As Boolean
Public SSCOE As Boolean
Public ALLDONE As Boolean
Option Explicit
 
Sub Grab_Info()
 
Week = Range("A1").Value
 
Stopped = False
UserFormShrink.Show
If Stopped Then GoTo StopNow
 
Application.DisplayAlerts = False
 
STOPSSCOE = False
UserFormSSCOE.Show
If STOPSSCOE Then GoTo StopNow
If SSCOE Then GoTo NoMore
If RunSSCOE Then GoTo Group1
 
Group1: Sheets("temp").Visible = True      'Unhide temp sheet add data, copy average shrinkage and rehide temp"
Sheets("temp").Activate
Cells.Select
ActiveSheet.Paste
Range("A300:AC303").Select
Selection.Copy
 
Sheets("site averages").Visible = True
Sheets("site averages").Activate
Range("b1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("temp").Visible = False
Sheets("site averages").Visible = False
 
NoMore: MsgBox "No line groups remain. Wizard complete!"
 
StopNow: Sheets("Attrition").Activate
Range("A1").Select
 
Application.DisplayAlerts = True
 
Exit Sub
End Sub

As you might be able to tell my co-worker only needs this to calculate the shrinkage of one group where as I was using it with 6 groups and had a seperate userform and similar code for each group. And it always worked perfectly allowing me to skip a group cancel the wizard or continue as normal based on the button I would hit. But now if I hit Cancel or CommandButton2 which = Stopped it shows I have hit CommandButton2 unloads the user form and then acts as if Stopped still = False continueing on with the code rather then going to the StopNow line I indicated it should. Am I missing something simple here? Cause I have tried anything and everything I can think of with no luck all the while the workbook I originally wrote this in everything works just fine...:(


only thing I can see if the below:

"If Stopped Then GoTo StopNow" i beleive should be
If Stopped = (true or false which ever) then GoTo StopNow

same with the below
If STOPSSCOE Then GoTo StopNow
If SSCOE Then GoTo NoMore
If RunSSCOE Then GoTo Group1

what are you testing? also I dont see end if or elses, not sure if this is all your code.
 
Upvote 0
You have declared Stopped in both modules. Try removing the declaration from the UserForm module.

That did indeed solve the problem. Oddly the exact same code worked in a differnet spreadsheet where it was created... Oh well problem solved and thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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