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 in first column into listbox on userform

0

Hi Experts ,

I search for sombody to  help me for my problem !

I make new project  when fill data in textboxes and comboxes and click commandbutton then will copy to listbox , but  I want in first column in listbox should autonumber 1,2,3.. evrey time  when copy to listbox , but just show 1 for the first row and when copy data  more to listbox   will be empty in first column (to more understand just try filling  field textboxes and combobox and  copy more than one time  will note the problem) .

here is the code 

Private Sub AddToList_Click()
    Dim i As Long
    For i = 1 To 3
        If Me("textbox" & i) = "" Then
            MsgBox Me("label" & i).Caption & " is missing", vbCritical: Exit Sub
        End If
    Next
    With Me.ListBox1
        .ColumnCount = 5
        .ColumnWidths = "80;100;80;80;80"
        .AddItem
        .List(.ListCount - 1, 1) = Me.ComboBox1
        .List(.ListCount - 1, 2) = Me.TextBox1.Value
        .List(.ListCount - 1, 3) = Me.TextBox2.Value
        .List(.ListCount - 1, 4) = Me.TextBox3.Value
        .List(lindex, 0) = lindex + 1
        For i = 0 To .ListCount - 1
            .List(i, 2) = Format(.List(i, 2), "#,##0.00")
            .List(i, 3) = Format(.List(i, 3), "#,##0.00")
            .List(i, 4) = Format(.List(i, 4), "#,##0.00")
        Next i
    End With
End Sub

thanks 

Answer
Discuss

Answers

0
Selected Answer

Hi again Mussa

You just forgot to declare the variable lindex at the Userform level (so it is retained between additions) and to add to it once a new line is written to the ListBox

This is done in the attached revised file by the li9nes in bold below:

Dim lindex As Long

Private Sub AddToList_Click()
    Dim i As Long
    For i = 1 To 3
        If Me("textbox" & i) = "" Then
            MsgBox Me("label" & i).Caption & " is missing", vbCritical: Exit Sub
        End If
    Next

    With Me.ListBox1
        .ColumnCount = 5
        .ColumnWidths = "80;100;80;80;80"
        .AddItem
        .List(.ListCount - 1, 1) = Me.ComboBox1
        .List(.ListCount - 1, 2) = Me.TextBox1.Value
        .List(.ListCount - 1, 3) = Me.TextBox2.Value
        .List(.ListCount - 1, 4) = Me.TextBox3.Value
        .List(lindex, 0) = lindex + 1
        lindex = lindex + 1
        For i = 0 To .ListCount - 1
            .List(i, 2) = Format(.List(i, 2), "#,##0.00")
            .List(i, 3) = Format(.List(i, 3), "#,##0.00")
            .List(i, 4) = Format(.List(i, 4), "#,##0.00")
        Next i
    End With
End Sub

Also I added the code to calculate the total automatically, using these code extracts:

Private Sub TextBox1_Change()
    If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
        TextBox3.Text = Format(CDbl(TextBox1.Text) * CDbl(TextBox2.Text), "#,###.00")
    End If
End Sub
Private Sub TextBox2_Change()
    If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
        TextBox3.Text = Format(CDbl(TextBox1.Text) * CDbl(TextBox2.Text), "#,###.00")
    End If
End Sub

(although you might want to use the AfterUpdate event instead).

Hope this fixes your problem. If so, please remember to mark this Answer as Seelected.

Discuss

Discussion

Hi John,
that's really what I want it.
thank you so much .
Mussa (rep: 48) Jan 4, '24 at 2:36 am
Good. Thanks for selecting my Answer, Mussa. 
John_Ru (rep: 6142) Jan 4, '24 at 2:39 am
Add to Discussion


Answer the Question

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