Procedure stopping after first line of code

Keojey

New Member
Joined
Sep 19, 2018
Messages
35
Office Version
  1. 365
Hello,

I'm messing around with Userforms and have been trying to get all the values of textboxes within' my Userform to save and match up with what's in the worksheet its supposed to be editing.

I took a very basic approach to this and have the start of my code looking like this :

VBA Code:
Private Sub CommandButton1_Click()

Sheets("Sheet1").Range("B4") = TextBox1.Value
Sheets("Sheet1").Range("B5") = TextBox2.Value

End Sub

Private Sub UserForm_Initialize()

TextBox1.Value = Sheets("Sheet1").Range("B4")
TextBox2.Value = Sheets("Sheet1").Range("B5")

End Sub

The issue I'm having is that only the first line of code is being registered when the CommandButton1 is being clicked. So the value of TextBox1 will change properly, but TextBox2 and anything added beyond this code gets ignored.
The Initialization for the Userform copies the text on the worksheet into the text boxes just fine.

Just as an example :
B4 = "ABC"
B5 = "123"

Opening the Userform has TextBox1 = "ABC" and TextBox2 = "123"

Changing TextBox1 to equal "DEF" and TextBox2 to equal "456" and pressing the CommandButton1 in order to apply these changes to the Worksheet

Only TextBox1's change to "DEF" saved, but it skipped over the TextBox2's change to "456."

No errors occur at any point.

I have a much larger quantity of fields I want entered in this Userform but can't seem to get over this fundamental problem.. Could anyone possibly guide me in the right direction of how I should have the code set-up instead so it doesn't do this?

Thank you!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
There is nothing wrong with that code & it should work.
Do you have any other code that you haven't shown?
 
Upvote 0
I didn't think it'd make a difference because it went through it anyways, but I have it so the current worksheet becomes unprotected then re-protected at the end.

So it looks a little more like this at the moment :

VBA Code:
Private Sub CommandButton1_Click()

Sheet1.Activate

ActiveSheet.Unprotect "1"

Sheets("Sheet1").Range("B4") = TextBox1.Value
Sheets("Sheet1").Range("B5") = TextBox2.Value

ActiveSheet.Protect "1"

End Sub

Private Sub UserForm_Initialize()

TextBox1.Value = Sheets("Sheet1").Range("B4")
TextBox2.Value = Sheets("Sheet1").Range("B5")

End Sub

The sheet unprotects, does its first TextBox value, then stops. It never gets to the point of locking the sheet again.
 
Upvote 0
Is the sheet name Sheet1, or is that the sheet codename?
 
Upvote 0
Just for example purposes, I renamed everything to "Sheet1".. But I noticed the first list in my code still says "Sheet1.Activate" even though it's not actually called Sheet1, so I can probably just delete this line. I tried taking out everything other than the example I originally posted and still had the same issue, however.

There is one major thing I believe I left out though: The worksheet's information I'm trying to update with this Userform is already being updated via a Worksheet_Change sub for other parts of the worksheet. I believe I'd just need to squeeze in a little code to disable that while the Userform works it's magic, correct?
 
Upvote 0
...I feel a little silly being stuck on this for so long, but just doing this fixed things

VBA Code:
Private Sub CommandButton1_Click()

Application.EnableEvents = False

Sheets("Sheet1").Range("B4") = TextBox1.Value
Sheets("Sheet1").Range("B5") = TextBox2.Value

Application.EnableEvents = True
End Sub
 
Upvote 0
I'd just need to squeeze in a little code to disable that while the Userform works it's magic, correct?
That's right & you can do that like
VBA Code:
Application.EnableEvents = False
With Sheets("Sheet1")
   .Unprotect "1"

   .Range("B4") = TextBox1.Value
   .Range("B5") = TextBox2.Value

   .Protect "1"
End With
Application.EnableEvents = True
 
Upvote 0
Solution

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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