Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

increment of numbers into textboxes based on selected comboboxes


Hi guys 

I  try  autonumbering numbers  into texboxes  based on selected from comboboxes.

so  if  I  select   item from combobox1  should  autonumber into  textbox1 = 1 ,if  I  select   item from combobox2  should  autonumber into  textbox2 = 2  and  so  on for  each   next   selected combobox   should  add 1 based on  previous autonumbering textbox 

this  is  what  I  have 

Private Sub UserForm_Initialize()
If ComboBox1.Value <> "" Then
TextBox1.Value = Application.Max(TextBox1.Value + 1)
End If
End Sub

nothing work  so  far .

hope  somebody  help.



Selected Answer

Hi Halk.

You didn't attach a file so I'm not sure what your form looks like but you should use the Initialize sub to set up the Userform (only). In the attached file, there's a button "Show form" which triggers this:

Private Sub CommandButton1_Click()
End Sub

which triggers this (and populates the 3 comboboxes on my form):

Private Sub UserForm_Initialize()

ListArr = Array("CCL-1004", "CCL-1005", "CCL-1006", "CCL-1007")

ComboBox1.List = ListArr
ComboBox2.List = ListArr
ComboBox3.List = ListArr

End Sub

When ComboBox1 is changed (a value selected), this code runs:

Private Sub ComboBox1_Change()

' send the number to the sub

WriteTB (1)

End Sub

It passes the value 1 to this sub which writes it to TextBox1:

Private Sub WriteTB(n As Long)
' set the textbox value to that sent....
Me.Controls("TextBox" & n).Value = n

End Sub

It works the same for whatever number is passed to it so your other Combobox Change subs are just:

Private Sub ComboBox2_Change()

WriteTB (2)

End Sub

Private Sub ComboBox3_Change()

WriteTB (3)

End Sub

Once your form is populated, the Submit button operates this (where you need to change the bold text with code to do what you want). 

Private Sub CommandButton1_Click()

'do whatever with the values of comboboxes etc.

Unload Me

End Sub

Hope this helps.



Hi John ,
You didn't attach a file so I'm not sure what your form looks like
sorry  about  it 
you  extremely  understood  all  of  my  requirements 
many  thanks  for  your  help  !
Halk (rep: 16) Jun 15, '22 at 11:42 am
Add to Discussion

Answer the Question

You must create an account to use the forum. Create an Account or Login