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

auto number for first column for each name in listbox

0

Hello,

I would auto number for each name  in listbox.

I will fill textboxes and copy to listbox . currently will auto number 1,2,3  consecutively 

but what I want when add new name for second column  then will start again 1,2,.... until add new name .so autonumbering  begins  from name in column 2 and elements in column 4 until finshing last elemnt in column 4 for the same name.

I put example in sheet how should show in first column after add new name in listbox .

important: in textbox1,2 will not repeat adding same name,invoice no  after fill from first time , just I add elemnts in column 4 for the same name .

Dim lindex As Long
Private Sub CommandButton1_Click()


Dim idx As Long

    With Me.ListBox1
        .AddItem

        For idx = 1 To 6
            .List(.ListCount - 1, idx) = Me.Controls("TextBox" & idx)

        Next idx
        .List(lindex, 0) = lindex + 1

                 lindex = lindex + 1

    End With
End Sub

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi again Mussa

Revision #1, 21 April 2025

Following user clarification (in the Discussion below), the revised code below  will "reset" autonumbering in ListBox1 when the name changes  but not repeat the same name in a subsequent ListBox addition with the same Name. Note that this uses a new variable LastName, declared at the top and not lost between calls on the code- see changes and explanatory comments in bold:

Dim lindex As Long, LastName As String

Private Sub CommandButton1_Click()


Dim idx As Long

    With Me.ListBox1
        .AddItem
        
        For idx = 1 To 6
            .List(.ListCount - 1, idx) = Me.Controls("TextBox" & idx)
        Next idx
        
        ' check if name matches (any) previous entry
        If .List(.ListCount - 1, 1) <> LastName Then
            ' reset lindex and save Name
            lindex = 0
            LastName = .List(.ListCount - 1, 1)
            
            Else
            ' clear name
            .List(.ListCount - 1, 1) = ""
        End If
        ' increment index and add as "auto number"
        lindex = lindex + 1
        .List(.ListCount - 1, 0) = lindex
    End With
End Sub

That code is in the (replaced) attached, revised file (...v0 b.xlsm).

I also changed the other code so that TextBox6 is updated for changes to TB5 or TB6:

Private Sub TextBox5_Change()
    ' check there's a Qty and Ubit Price
    If IsNumeric(TextBox4.Text) = False Or IsNumeric(TextBox5.Text) = False Then
        TextBox6.Text = ""
        Exit Sub

        Else
        TextBox6.Value = Format(CDbl(TextBox4.Text) * CDbl(TextBox5.Text), "#,###.00")
    End If
End Sub

Private Sub TextBox4_Change()
    ' do same as if QTY changed
    Call TextBox5_Change

End Sub

Revision #2, 23 April 2025

Following a user comment (that the above code chnnged nothing), the file (...v0 b.xlsm) has been replaced by an identical file but now including a sample screen grab of the ListBox results from the above code- see new file named:

items in listbox with screengrab v0 c.xlsm

Hope this fixes things for you- if so, please remember to mark this Answer as Selected.

Discuss

Discussion

Hi john again, I hope you're fine.
I tested and will show 
1
1
2
when add 3 IDs in column 4(three rows) for the name
the correct should be 
1
2
3
also when there are 2 IDs in column 4 (two rows) for the name then will show in first column 
1
1
the correct should be
1
2
Mussa (rep: 50) Apr 20, '25 at 9:35 am
Sorry Mussa, I replied in a hurry. Will look again tomorrow and correct the code. 
John_Ru (rep: 6722) Apr 20, '25 at 9:41 am
Mussa

Did you use the button on the ListBox in my file? It seems to work fine for me or at least as I understand your Question to require)...

If I add 3 items then chnage the name and add 2 more, the ListBox shows something like this: once, I get
1      John      123      TYH      7      78      546.00
2      John      123      TYH      8      78      624.00 
3      John      123      TYH      10      78      780.00 
1      Willie    123      YYYY      2    6.50       13.00 
2      Willie    123      YYYY     4    8.50      34.00

If I change the name everytime. each new row starts with 1.

Please check and reply.

(Note that I editted my Answer earlier but only to correct the spelling of "autonumbering")
John_Ru (rep: 6722) Apr 21, '25 at 7:20 am
If I add 3 items then chnage the name and add 2 more, the ListBox shows something like this: once, I get
this is because of  you repeat the same name and show in column2 , but I mentioned 
important: in textbox1,2 will not repeat adding same name,invoice no  after fill from first time , just I add elemnts in column 4 for the same name .
the same name will be like this:
1      John      123      TYH      7      78      546.00
2                123      TYH      8      78      624.00 
3                123      TYH      10      78      780.00 
 
Mussa (rep: 50) Apr 21, '25 at 8:53 am
Mussa. Thanks - that was the unintelligle bit of your Question (particularly since you'd used a simple loop to populate all "columns" of the ListBox).

I'll try to look at it again later. 
John_Ru (rep: 6722) Apr 21, '25 at 1:32 pm
Mussa, please see "Revision #1, 21 April 2025" to my Answer plus the revised file.
John_Ru (rep: 6722) Apr 21, '25 at 3:35 pm
I don't think to change anything,sorry!
Mussa (rep: 50) Apr 22, '25 at 5:52 am
Mussa- if you mean my revised Answer didn't change anything, you're wrong! Please try the new file I'd attached- it works quite differently to the original file (I just tried it from this web page to check)..
John_Ru (rep: 6722) Apr 22, '25 at 3:13 pm
I'm pretty sure !
 yesterday and today I downloaded  your attachment and the problem is still continued.
Mussa (rep: 50) Apr 23, '25 at 3:58 am
Okay I'll check soon but I was getting ListBox results like you requested above:    
1      John      123      TYH      7      78      546.00
2                123      TYH      8      78      624.00 
3                123      TYH  

... and restart to 1 if the name was changed. 
John_Ru (rep: 6722) Apr 23, '25 at 5:42 am
Strange!!
this what I got 
1       MUSSA        ASD111       DF4      20     10    200
1                                 DF5      10     10    100
1       MUSSI        ASD1222      FGT5     10     10    100
1                                 FGT15    10     11    110
2                                 FGT115   10     20    200
Mussa (rep: 50) Apr 23, '25 at 6:45 am
Mussa- can't see anything in your item above (you can't add images in this Forum) but please see Revision #2, 23 April 2025 to my Answer - without changing anything, I get the results shown in replacement file named items in listbox with screengrab v0 _c.xlsm
John_Ru (rep: 6722) Apr 23, '25 at 6:49 am
sorry John !
I've found my bad !
when I copy data for the same name  I clear name  from textbox1  that's why doesn't auto numbering  correctly .
now the code works as I would.
thank you.
Mussa (rep: 50) Apr 23, '25 at 4:05 pm
Mussa- glad that worked for you and thanks for selecting my Answer. 
John_Ru (rep: 6722) Apr 23, '25 at 4:43 pm
Add to Discussion


Answer the Question

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