Forum Discussion
nasser1982
Aug 22, 2023Copper Contributor
عدم ظهور البيانات في اليوزر فور
I'm having a problem fetching data into the laser form, as it shows the data of the last cell in the excel sheet according to the code
the next :
On Error Resume Next
If Me.Tx16.Value <> "" Then
Me.Li1.Visible = True
Else
Me.Li1.Visible = False
Exit Sub
End If
worksheet2.Range("Q2").Value = Tx16.Text
Me.Li1.Clear
Me.Li1.Height = 77
Dim c, lr As Integer
lr = worksheet2.Range("b1000").End(xlUp).Row
With worksheet2
For c = 2 To lr
a = Len(Me.Tx16.Text)
If Left(worksheet2.Cells(c, "b").Value, a) = Left(Me.Tx16.Text, a) Then
Me.Li1.AddItem worksheet2.Cells(c, "b").Value
End If
Next c
End With
End Sub
- NikolinoDEGold Contributor
It seems like you are working on a VBA code in Excel to fetch data from a worksheet and display it in a userform listbox (Li1). Based on the provided code snippet, you want to display items from column B of worksheet2 in the listbox that match the beginning of the text entered in Tx16. However, you mentioned that the code displays the data of the last cell in the Excel sheet.
To help you resolve this issue, I have reviewed your code and identified a potential problem:
The issue could be related to the line Me.Li1.Clear, which clears all items from the listbox (Li1) right after setting its height. If you want to display the filtered items in the listbox, you should avoid clearing it before adding items.
Here is a modified version of your code:
On Error Resume Next If Me.Tx16.Value <> "" Then Me.Li1.Visible = True Else Me.Li1.Visible = False Exit Sub End If worksheet2.Range("Q2").Value = Tx16.Text Me.Li1.Height = 77 Me.Li1.Clear ' Clearing the listbox is not necessary here Dim c As Integer, lr As Integer lr = worksheet2.Range("B1000").End(xlUp).Row With worksheet2 For c = 2 To lr a = Len(Me.Tx16.Text) If Left(.Cells(c, "B").Value, a) = Left(Me.Tx16.Text, a) Then Me.Li1.AddItem .Cells(c, "B").Value End If Next c End With
In this modified code, I have removed the Me.Li1.Clear line, so the listbox items will not be cleared before adding new items. This should allow the listbox to display the filtered items correctly. If you still encounter issues, please provide more details about the problem (insert a file) and your digital environment. Digital environment like Excel version, operating systems, storage medium, etc.
The text and the steps are the result of various AI's put together.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark them as helpful!
This will help all forum participants.