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.
 
Or try something like this:

VBA Code:
Private Sub CommandButton1_Click()
'Modified  1/5/2022  7:19:00 PM  EST
ListBox1.ColumnCount = 2
ListBox1.List = Range("A1:B25").Value


End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Not sure what this means:
.RowSource = "'" & oWs.Name & "'!A" & lStart & ":" & "Q" & lFinish

Tell me in words what this means:

Are you showing a sheet name and the range

So tell me in words the sheet name and the Range
 
Upvote 0
Not sure what this means:
.RowSource = "'" & oWs.Name & "'!A" & lStart & ":" & "Q" & lFinish

Tell me in words what this means:

Are you showing a sheet name and the range

So tell me in words the sheet name and the Range

that is for load data to listbox, i create pagination mechanism in my form, so user can click what ever page they want and that code will bring to them the data based on the page number, i create formula for that (to count where row to start and where row to finish) example : if user click page 2, then formula to count lStart and lFinish will automatic give row 26 and 50 (since my data start from row 2, row 1 is header, and i give them limitation to 25 data / page) and oWs name is name of worksheet i use to save data.
 
Last edited:
Upvote 0
Thanks for that explanation even if I do not understand it.

I'm sure someone else here on the forum may be able to help you.
I will keep watching. Take care.
 
Upvote 0
Thanks for that explanation even if I do not understand it.

I'm sure someone else here on the forum may be able to help you.
I will keep watching. Take care.

i use oWs.Name instead "Sheet1" or "Sheet2" because i need it to be dynamic, so data for listbox not only from one specific sheet, it can be from anywhere.
 
Upvote 0
i use oWs.Name instead "Sheet1" or "Sheet2" because i need it to be dynamic, so data for listbox not only from one specific sheet, it can be from anywhere.
That's why I would need to see all your code.

Like you said this:
i populate the listbox with RowSource

How do you do that?
Do you go into the Properties window and modify it there?
 
Upvote 0
VBA Code:
Public Sub LoadListBox(ByRef oList As MSForms.ListBox, ByVal iLimit As Byte)
    Dim oWs As Worksheet
    Dim lRow As Long
    
    Set oWs = ThisWorkbook.Sheets(strSheet)
    
    lRow = oWs.UsedRange.Rows.Count
    
    oList.RowSource = vbNullString
    
    If lRow > 1 Then
        Dim lStart As Long
        Dim lFinish As Long
        
        lStart = iPage * iLimit + 2 - iLimit
        lFinish = lStart + iLimit - 1
    
        If lRow < lFinish Then lFinish = lRow
            
        With oList
            .ColumnCount = 17
            .ColumnHeads = False
            .ColumnWidths = "120,120,200,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
            .RowSource = "'" & oWs.Name & "'!A" & lStart & ":" & "Q" & lFinish
            .Selected(0) = True
        End With
    End If
    
    Set oWs = Nothing
End Sub

strSheet is public variable (string) to handle changing of sheet in use.
iPage is public variable (integer) to handle what page is user click.
 
Upvote 0
since ListBox_Change event and "If ListBox.ListCount > 0" not automatically clear my textboxes from the data before re-populate, it's seems i have to do manually clear it with TextBox = VbNullString, for 17 text boxes.

i will let this thread unsolved btw.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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