populating data show error in textboxes number on userform

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have about 24 textboxes when I select the item from listbox should populate from textbox 1 to textbox24 , but it populates staring from textbox 3,
the textbox1,2 is empty !!! what's the mistake?
VBA Code:
Private Sub ListBox1_Click()
For i = 0 To ListBox1.ListCount
    If ListBox1.Selected(i) = True Then
        For j = 1 To 24
        Controls("TextBox" & j).Text = Cells(ListBox1.List(i, 1), j)
        Next j
    End If
Next i
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Hasson. For "i", the Listbox1 does have List item 0 but not List item Listbox1.ListCount. That doesn't resolve your issue which I suspect is related to your use of "Cells". Try qualifying it with a sheet name ie. Sheets("Sheet1").Cells etc. You may also want to qualify your controls ie. Userform1.Listbox1; Userform1.Controls etc. U also have to have all the listbox items selected before the textboxes can be filled? Also, U check your textbox names. HTH. Dave
 
Upvote 0
any solutin for fix this problem ?

untested but try following update to your code & see if resolves the issue

VBA Code:
Private Sub ListBox1_Click()
    Dim i           As Long
  
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) = True Then
                For j = 1 To .ColumnCount
                    Me.Controls("TextBox" & j).Text = .List(i, j - 1)
                Next j
            End If
        Next i
    End With
  
End Sub

Dave
 
Upvote 0
thanks Dave !
unfortunately just fill textbox1 and the others become empty .
 
Upvote 0
curious - you probably have something else going on.

Publishing all the userforms codes would be helpful to forum & copy of worksheet with dummy data using MrExcel Addin xl2BB

Dave
 
Upvote 0
this is the whole codes
VBA Code:
Private Sub ListBox1_Click()
For i = 0 To ListBox1.ListCount
    If ListBox1.Selected(i) = True Then
        For j = 1 To 24
        Controls("TextBox" & j).Text = Cells(ListBox1.List(i, 1), j)
        Next j
    End If
Next i
End Sub

Private Sub TextBox25_Change()
ListBox1.Clear

    For i = 1 To 24
            Controls("TextBox" & i).Text = ""
    Next i

    If TextBox25 = "" Then Exit Sub
    Sheets("file1").Activate

    ss = Sheets("file1").Cells(Rows.Count, 6).End(xlUp).Row
     k = 0
     
     
For Each C In Range("f10:f" & ss)
    If C Like TextBox25.Value & "*" Then
        ListBox1.AddItem
        ListBox1.List(k, 0) = Cells(C.Row, 6).Value
        ListBox1.List(k, 3) = C.Row
        k = k + 1
    End If
Next C


End Sub

Private Sub UserForm_Activate()
TextBox25.SetFocus
For i = 10 To Sheets("file1").Cells(Rows.Count, 6).End(xlUp).Row
ListBox1.AddItem
 ListBox1.List(i - 10, 0) = Cells(i, 6).Value
  ListBox1.List(i - 10, 1) = i
      Next i
End Sub
when populate data strat from column C from row 10 to column Z
 
Upvote 0
when populate data strat from column C from row 10 to column Z
in that case try following

VBA Code:
Private Sub ListBox1_Click()
    Dim i As Long, j As Long
   
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) = True Then
                For j = 1 To 24
                    Me.Controls("TextBox" & j).Text = Cells(i + 10, j + 2).Text
                Next j
            End If
        Next i
    End With
   
End Sub

range is unqualified & assumes sheet with data is the activesheet

Dave
 
Upvote 0
Solution
every thing is great !
much appracite for your help :)
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,285
Members
449,218
Latest member
Excel Master

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