Problem getting correct Listbox selected item row

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to identify the row number of a Listbox selected item;

Code:
'All Records
If ComboBox1.Value = "All Records" Then
CmdView.Enabled = True
Total = Sheet9.Range("Q1").Value
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
TextBox4.Value = .List(i, 14)
Set FindRow = Sheet9.Range("O2:O500").Find(What:=TextBox4.Value, LookIn:=xlValues)
TextRow.Value = FindRow.Row
TextBox1.Value = .List(i, 0)
TextBox2.Value = Format(.List(i, 2), "dd mmmm yyyy")
TextBox3.Value = .List(i, 13)
CommandButton4.Enabled = True
End If
Next i
End With
End If

The first row is a header, hence searching for the value from row 2 onwards. In this particular test I have 2 rows of data, each having the value required so would expect to find 2 entries, (which I do), and in theory they should be identified as being on row 2 and 3 respectively.

The issue I'm having is that the very first item in the Listbox always produces a row number of 3, when in this example it should be 2 then 3.

Can anyone show me where this is going wrong please?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
i can see you are using .listcount - 1, but where is .listcount being created and what is the original values, possibly something there
 
Upvote 0
Hi,

Not sure I understand the question - but the code I posted is being triggered on the Listbox.Click event - do you need to know how the Listbox is loaded? If so here;

Code:
Dim LastRow As Long

LastRow = Range("D65536").End(xlUp).Row '+ 10
'All
If ComboBox1.Value = "All Records" Then
CmdEdit.Enabled = False
CmdView.Enabled = False
Dim rng As Range, Dn As Range, Ac As Long, c As Long
Label15.Visible = True
Label16.Visible = True
Label16.Caption = "121 Date"
Label17.Visible = True
Application.ScreenUpdating = False
Sheet9.Activate
With Sheet9
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = False
Next i
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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