0

I am running a Userform in Excel VBA and it seems to work, however, it doesn't seem to close. Basically, once the data is input into the form and a "SUBMIT" button is clicked, I would like the data stored in certain cells and close the form as well as run another sub code.

The following is my VBA code:

Private Sub CommandButton1_Click()
HaulerRatesForm.Label1.Caption = Worksheets("Dashboard").Range("A47").Value
HaulerRatesForm.Label2.Caption = Worksheets("Dashboard").Range("A48").Value
HaulerRatesForm.Label3.Caption = Worksheets("Dashboard").Range("A49").Value
HaulerRatesForm.Label4.Caption = Worksheets("Dashboard").Range("A50").Value

HaulerRatesForm.Show

End Sub

Private Sub UserForm_Initialize()

End Sub

Private Sub CommandButton2_Click()
Worksheets("Dashboard").Cells(47, "H").Value = HaulerRatesForm.TextBox1.Value
Worksheets("Dashboard").Cells(48, "H").Value = HaulerRatesForm.TextBox2.Value
Worksheets("Dashboard").Cells(49, "H").Value = HaulerRatesForm.TextBox3.Value
Worksheets("Dashboard").Cells(50, "H").Value = HaulerRatesForm.TextBox4.Value

Unload Me

Call Dashboardcodes2

End Sub

I can't seem to figure out why Unload Me doesn't seem to close the window. Any ideas as to what I'm doing wrong here?

6
  • Where is this code located? is the other code running. What does the procedure do that runs after the unload? Is there a terminate procedure? The problem will be more clear if you try stepping through the code with F8. See this link for more VBA Debugging techniques from the amazing Chip Pearson. Commented Jun 10, 2018 at 0:45
  • Yes, the other code is running fine - I tested it without having the form in place. It's a long code and thought it may occupy unnecessary space in the question and did not add it there. There's no problem in the code, just the form. To clarify, the form was added on to a running code. They are all part of a massive project I'm working on. Sub Dashboardcodes2 () comes right after this. Commented Jun 10, 2018 at 1:00
  • I am using Application.ScreenUpdating = False at the beginning of the code, however - whould that affect the form? Commented Jun 10, 2018 at 1:14
  • It could -- and in the time it took you to ask that, you could have tried commenting it out and see. Here's here gist of troubleshooting: remove/comment-out as much code as you can, as much as necessary to make the problem not occur. Then, start adding it back in, one line at a time, until you know which part was the problem. Commented Jun 10, 2018 at 1:45
  • 1
    This may be a duplicate of this question from yesterday. Commented Jun 10, 2018 at 2:31

1 Answer 1

1

Thanks to urdearboy, this answered previously seems to answer my question.

I used the below code by navigating directly to the userform's button by double-clicking on it:

Private Sub CommandButton2_Click()
Worksheets("Dashboard").Cells(47, "H").Value = HaulerRatesForm.TextBox1.Value
Worksheets("Dashboard").Cells(48, "H").Value = HaulerRatesForm.TextBox2.Value
Worksheets("Dashboard").Cells(49, "H").Value = HaulerRatesForm.TextBox3.Value
Worksheets("Dashboard").Cells(50, "H").Value = HaulerRatesForm.TextBox4.Value

Unload Me

End Sub

I placed the code in Dashboardcodes2 under Unload Me and everything is now working perfectly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.