How to add content to Excel VBA ListBox
Hello, I am with a doubt in a code for Excel VBA. I want the code to search the sheet for the same number that was entered in TextBox
, after finding it sends the information from the entire line of the sheet to ListBox
. Since the code is contained in the AfterUpdate
subroutine of the Textbox
, it resets the contents of the ListBox
by rewriting every time in line 0. I don't know if I was clear. I appreciate it now.
Private Sub TextBox1_AfterUpdate()
Dim linhalistbox As Integer
Dim linha As Integer
Dim i As Integer
Dim total As Double
linhalistbox = 0
linha = 2
i = 1
total = 0
Do Until Plan2.Cells(linha, 1) = ""
If TextBox1.Text = Plan2.Cells(linha, 1) Then
With Me.ListBox1
.AddItem
.List(linhalistbox, 0) = i
.List(linhalistbox, 1) = Plan2.Cells(linha, 3)
.List(linhalistbox, 2) = Plan2.Cells(linha, 4)
TextBox2.Text = i
TextBox5.Text = Plan2.Cells(linha, 4)
total = total + CDec(TextBox5.Text)
TextBox6.Text = total
linhalistbox = linhalistbox + 1
i = i + 1
End With
End If
linha = linha + 1
Loop
TextBox1.Text = ""
End Sub
0
Author: Victor Stafusa, 2018-07-15
1 answers
Problem
You are setting the elements in the listbox from the item with index 0
Dim linhalistbox As Integer
linhalistbox = 0
Solution:
Initializes the variable with the total of elements already present in the listbox
Dim linhalistbox As Integer
linhalistbox = Me.ListBox1.ListCount
1
Author: Jorge Costa, 2018-07-15 09:10:30