VBA Populate Listbox / Edit Listbox based on Criteria on my Textbox1

djossh

Board Regular
Joined
Jul 27, 2009
Messages
243
I have data in sheet4 (contains 8 columns), I need to populate my list box using criteria from my textbox1
After populating my listbox I will select the cell or data that I need to edit (From the Listbox) and save it on my worksheet.

SAMPLE DATA
ABCDEFGH
1123XXX1212SCFDSGREFFFGD
2123DFA5532SAGASADFGSSSGS
3123GASD24521FSDAGDGAEEEVB
4145ASDGA5851ASBAADGAAADF
5145HFSDF42GHASADGDDDGD
6222SHDFE6451SDGRHSDJJJTE
7222ERRT3324FSSDRDDSKKKEW
8222HFS2425SHRESDFGGGWE
9555RTHFS2464FADGTSDFHRRRFT
11555TRER242FSADSDGERHWWWJH

<tbody>
</tbody>


SAMPLE LISTBOX below, based on my criteria from my textbox1 = 145 (column A)
here if i will edit something (except column A) I want the changes to be save/replace the previous data.

145ASDGA5851ASBAADGAAADF
145HFSDF42GHASADGDDDGD

<tbody>
</tbody>

Thank you in advance.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I'm assuming you're some what familiar with userforms, considering you have a textbox and listbox already.

To get listbox1 to populate based on testbox1:
Code:
Private Sub TextBox1_Change()


Dim TBL() As Variant
Dim BB As Integer
Dim CC As Integer
Dim DD As Integer


TBL = Sheet4.Range("A1:H11")
UserForm1.ListBox1.Clear
DD = 0
For BB = 1 To UBound(TBL, 1)
    If Val(TextBox1.Value) = Val(TBL(BB, 1)) Then
        DD = DD + 1
        With UserForm1.ListBox1
            .ColumnCount = 8
            .AddItem
            For CC = 1 To 8
               .Column(CC - 1, DD - 1) = TBL(BB, CC)
            Next CC
        End With
    End If
Next BB


End Sub

The only way I've found to be able to edit the data in the listbox, is to create 7 more textboxes (TextBox2 throught Textbox 8) and a commandbutton (CommandButton1). When the item is selected in the ListBox, the rest of the textboxes are populated with the correct information. Then once you have changed whatever it is that you want to change, click the commandbutton and everything will update with what you want. I set the visibility of each of these new textboxes and the commandbutton to False, so they only show up when you select an item from the list box. Here is the code:

Code:
Private Sub CommandButton1_Click()


Dim TBL() As Variant
Dim BB As Integer
Dim CC As Integer
Dim DD As Integer
Dim EE As Integer


TBL = Sheet4.Range("A1:H11")
EE = 0


For BB = 0 To UserForm1.ListBox1.ListCount - 1
    If UserForm1.ListBox1.Selected(BB) Then
        For CC = 1 To UBound(TBL, 1)
            For DD = 1 To 8
                If Val(UserForm1.ListBox1.List(BB, DD - 1)) <> Val(TBL(CC, DD)) Then
                    Exit For
                Else
                    If DD = 8 Then
                        EE = 1
                    End If
                End If
            Next DD
            If EE = 1 Then
                Exit For
            End If
        Next CC
        Exit For
    End If
Next BB


If EE = 1 Then
    For DD = 2 To 8
        TBL(CC, DD) = UserForm1.Controls("TextBox" & DD).Value
        UserForm1.Controls("TextBox" & DD).Visible = False
    Next DD
    UserForm1.CommandButton1.Visible = False
    Sheet4.Range("A1:H11") = TBL
End If


TBL = Sheet4.Range("A1:H11")
UserForm1.ListBox1.Clear
DD = 0
For BB = 1 To UBound(TBL, 1)
    If Val(TextBox1.Value) = Val(TBL(BB, 1)) Then
        DD = DD + 1
        With UserForm1.ListBox1
            .ColumnCount = 8
            .AddItem
            For CC = 1 To 8
               .Column(CC - 1, DD - 1) = TBL(BB, CC)
            Next CC
        End With
    End If
Next BB
           
End Sub


Private Sub ListBox1_Click()


Dim BB As Integer
Dim CC As Integer


For BB = 0 To UserForm1.ListBox1.ListCount - 1
        If UserForm1.ListBox1.Selected(BB) Then
            For CC = 1 To UserForm1.ListBox1.ColumnCount
                UserForm1.Controls("TextBox" & CC).Visible = True
                UserForm1.Controls("TextBox" & CC).Value = UserForm1.ListBox1.List(BB, CC - 1)
            Next CC
            UserForm1.CommandButton1.Visible = True
        End If
Next BB


End Sub

I hope this is what you're looking for, or at least helps out a bit.
 
Upvote 0
I'm assuming you're some what familiar with userforms, considering you have a textbox and listbox already.

To get listbox1 to populate based on testbox1:
Code:
Private Sub TextBox1_Change()


Dim TBL() As Variant
Dim BB As Integer
Dim CC As Integer
Dim DD As Integer


TBL = Sheet4.Range("A1:H11")
UserForm1.ListBox1.Clear
DD = 0
For BB = 1 To UBound(TBL, 1)
    If Val(TextBox1.Value) = Val(TBL(BB, 1)) Then
        DD = DD + 1
        With UserForm1.ListBox1
            .ColumnCount = 8
            .AddItem
            For CC = 1 To 8
               .Column(CC - 1, DD - 1) = TBL(BB, CC)
            Next CC
        End With
    End If
Next BB


End Sub

The only way I've found to be able to edit the data in the listbox, is to create 7 more textboxes (TextBox2 throught Textbox 8) and a commandbutton (CommandButton1). When the item is selected in the ListBox, the rest of the textboxes are populated with the correct information. Then once you have changed whatever it is that you want to change, click the commandbutton and everything will update with what you want. I set the visibility of each of these new textboxes and the commandbutton to False, so they only show up when you select an item from the list box. Here is the code:

Code:
Private Sub CommandButton1_Click()


Dim TBL() As Variant
Dim BB As Integer
Dim CC As Integer
Dim DD As Integer
Dim EE As Integer


TBL = Sheet4.Range("A1:H11")
EE = 0


For BB = 0 To UserForm1.ListBox1.ListCount - 1
    If UserForm1.ListBox1.Selected(BB) Then
        For CC = 1 To UBound(TBL, 1)
            For DD = 1 To 8
                If Val(UserForm1.ListBox1.List(BB, DD - 1)) <> Val(TBL(CC, DD)) Then
                    Exit For
                Else
                    If DD = 8 Then
                        EE = 1
                    End If
                End If
            Next DD
            If EE = 1 Then
                Exit For
            End If
        Next CC
        Exit For
    End If
Next BB


If EE = 1 Then
    For DD = 2 To 8
        TBL(CC, DD) = UserForm1.Controls("TextBox" & DD).Value
        UserForm1.Controls("TextBox" & DD).Visible = False
    Next DD
    UserForm1.CommandButton1.Visible = False
    Sheet4.Range("A1:H11") = TBL
End If


TBL = Sheet4.Range("A1:H11")
UserForm1.ListBox1.Clear
DD = 0
For BB = 1 To UBound(TBL, 1)
    If Val(TextBox1.Value) = Val(TBL(BB, 1)) Then
        DD = DD + 1
        With UserForm1.ListBox1
            .ColumnCount = 8
            .AddItem
            For CC = 1 To 8
               .Column(CC - 1, DD - 1) = TBL(BB, CC)
            Next CC
        End With
    End If
Next BB
           
End Sub


Private Sub ListBox1_Click()


Dim BB As Integer
Dim CC As Integer


For BB = 0 To UserForm1.ListBox1.ListCount - 1
        If UserForm1.ListBox1.Selected(BB) Then
            For CC = 1 To UserForm1.ListBox1.ColumnCount
                UserForm1.Controls("TextBox" & CC).Visible = True
                UserForm1.Controls("TextBox" & CC).Value = UserForm1.ListBox1.List(BB, CC - 1)
            Next CC
            UserForm1.CommandButton1.Visible = True
        End If
Next BB


End Sub

I hope this is what you're looking for, or at least helps out a bit.

THANK YOU SO MUCH... I REALLY APPRECIATE YOUR HELP.. God Bless you...it was perfect..
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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