Get row number of each selected Listbox item

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I have a multi-select Listbox on a Userform and I need to get the row number of each selected item if someone can help please.
 

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.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Mar14
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rws [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]With[/COLOR] ListBox1
[COLOR="Navy"]For[/COLOR] n = 0 To .ListCount - 1
    [COLOR="Navy"]If[/COLOR] .Selected(n) [COLOR="Navy"]Then[/COLOR]
        Rws = Rws & IIf(Rws = "", n + 1, ", " & n + 1)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]If[/COLOR] Rws <> "" [COLOR="Navy"]Then[/COLOR] MsgBox "Selected Rows :- " & Rws
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mick, you're a legend - this has been driving me nuts for days...

Thanks!
 
Upvote 0
Mick - can you help me please with a further step?

I need to update column D on sheet1 for each selected row with the value in TextDate.
 
Last edited:
Upvote 0
Assuming you have more than one column in your listbox, what list box column do you what to use to update column "D" of the sheet., Or maybe you only have one column in your Listbox !!!!
Also
Do I assume that the listbox row numbers is the same row numbers as on the sheet.??
 
Upvote 0
Thanks Mick....

The Listbox row numbers are + 2 with regard to the sheet and what I need to do is update each row in Column D with the value from a Textbox named TextDate - there are 15 columns in the Listbox.

It's never that straightforward with me I'm afraid!
 
Upvote 0
So can I assume that column 4 in the List box is equivalent to column "D" on the sheet.
Also, How do the values in the listbox get to the Textbox ???
Also, why can't you just update the sheet from the selected items in the listbox ???
 
Upvote 0
Yes - column 4 is equivalent to D....
TextDate just has the current date in it - so what I'm trying to do is enter the current date on the sheet for the items selected in the Listbox....
I have no idea how to update the sheet direct from the Listbox!

Thanks Mick - appreciated.
 
Upvote 0
Try this code in you Userform Listbox.
The code just uses the Row reference from the Listbox selected items to paste the textbox Date into the appropriate cell in column "D" sheet1, using CommandButton 1.

Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rws [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]With[/COLOR] ListBox1
    [COLOR="Navy"]If[/COLOR] TextBox1 <> "" [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]For[/COLOR] n = 0 To .ListCount - 1
            [COLOR="Navy"]If[/COLOR] .Selected(n) [COLOR="Navy"]Then[/COLOR]
                '[COLOR="Green"][B]Nb:- Change "+ 3" as appropriate !![/B][/COLOR]
                Sheets("Sheet1").Cells(n + 3, "D").Value = TextBox1.Value
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] n
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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