code doesn't work in initialize userform

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
hi
I have this code doesn't work in initialize user form . it just work in commandbutton when I put this
VBA Code:
Private Sub CommandButton1_Click()

Dim n As Long, Rws As String
With ListBox1
For n = 0 To .ListCount - 1
    If .Selected(n) Then
        Rws = Rws & IIf(Rws = "", n + 1, ", " & n + 1)
    End If
Next n
If Rws <> "" Then
TextBox1.Value = Rws
MsgBox "Selected Rows :- " & Rws
End If
End With

End Sub

but when I put into initialize user form
Code:
Private Sub UserForm_Initialize()
  
    Dim n As Long, Rws As String
With ListBox1
.ListBox1.List = Sheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
For n = 0 To .ListCount - 1
    If .Selected(n) Then
        Rws = Rws & IIf(Rws = "", n + 1, ", " & n + 1)
    End If
Next n
If Rws <> "" Then
TextBox1.Value = Rws
MsgBox "Selected Rows :- " & Rws
End If
End With

End Sub
it doesn't work at all
my request when I select item from listbox automatically show the number of row in textbox1
any suggestion to fix it ,please ?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You need to put the code into the listbox click event.
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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