Clear TextBox value

LongToast

New Member
Joined
Dec 1, 2021
Messages
32
Office Version
  1. 2003 or older
Platform
  1. Windows
i had code like this :

VBA Code:
Private Sub ListBox1_Change()
    With Me.ListBox1
      
        If .ListCount > 0 Then
            Me.TextBox2 = .List(.ListIndex, 0)
            Me.TextBox5 = .List(.ListIndex, 1)
        End If

    End With
End Sub

i populate the listbox with RowSource, when listbox has data, those textboxes get value based on selection in listbox and it works perfectly, but when i re-populate the listbox and the listbox has NO data, those textboxes still show data before re-populate instead empty value or nullstring, i had try this :

VBA Code:
Private Sub ListBox1_Change()
    With Me.ListBox1
      
            Me.TextBox2 = vbNullString
            Me.TextBox5 = vbNullString

        If .ListCount > 0 Then
            Me.TextBox2 = .List(.ListIndex, 0)
            Me.TextBox5 = .List(.ListIndex, 1)
        End If

    End With
End Sub

but didn't works, it's like ListBox1_Change event not working properly, can someone explain this please.

thanks in advance.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Do not do this:
i populate the listbox with RowSource

Load the listbox with vba
 
Upvote 0
Something like this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  1/5/2022  6:48:00 PM  EST
ListBox1.List = Range("A1:A25").Value
End Sub
 
Upvote 0
Something like this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  1/5/2022  6:48:00 PM  EST
ListBox1.List = Range("A1:A25").Value
End Sub

sometimes i have to update the value in that range, if i use ListBox1.List data in listbox not get updated, i have to re-load manually, but if i use RowSource, i only need to update data in sheet and listbox will auto update.

is it the only solution? i mean use ListBox1.List?
 
Upvote 0
So what is in the Rowsource?

Or try something like this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  1/5/2022  6:54:00 PM  EST

With ListBox1
    .Clear
    .AddItem "Alpha"
    .AddItem "Bravo"
    .AddItem "Charlie"
End With

End Sub
 
Upvote 0
So what is in the Rowsource?

Or try something like this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  1/5/2022  6:54:00 PM  EST

With ListBox1
    .Clear
    .AddItem "Alpha"
    .AddItem "Bravo"
    .AddItem "Charlie"
End With

End Sub

VBA Code:
.RowSource = "'" & oWs.Name & "'!A" & lStart & ":" & "Q" & lFinish

i create something like pagination mechanism, my real data start from column A to Q in my sheet, for all i know, AddItem can only handle 10 column and i have 17 column, is it using array is the only option to make ListBox_Change and .ListCount > 0 working properly?
 
Upvote 0
I never realized you had a multicolumn Listbox.

This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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