Code opens wrongly selected customer from listbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Code in use is shown below.
In a userform i search for a customer.
My listbox is populated with the results.

I can select any customer in the listbox & that customers info is then shown on the screen no problem.
Having said that i have an issue where cell A5 is TOM JONES & TOM PETTY
If my search is for TOM i select TOM JONES from the listbox BUT TOM PETTY is shown on the screen.

I added 2 new rows so ANDY 1 is row A6 & ANDY 2 is row A5
I search for ANDY & select ANDY 1 inT ANDY 2 is loaded.

Do you see anything in the code that has an issue with the cell A5 value loading wrongly


Rich (BB code):
Private Sub ListBox1_Click()
     Dim rw As Long
     Dim ws As Worksheet
     Dim answer As Integer
     Set ws = ThisWorkbook.Sheets("Database")
    If ListBox1.ListIndex = -1 Then Exit Sub
     rw = ListBox1.List(ListBox1.ListIndex, 3)
     ws.Range("A" & rw).Select
     Unload Me
     
     
   answer = MsgBox("OPEN CUSTOMERS FILE IN MAIN DATABASE ?", vbYesNo + vbInformation, "OPEN DATABASE MESSAGE")
   If answer = vbYes Then
   
   Database.LoadData Sheets("DATABASE"), Selection.Row

    Else
    Unload DatabaseSearchForm
    End If
  End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hmmm
In cell A5 i see TOM A so i search for TOM & the results are shown in the listbox.
I select TOM A in the listbox but i then see the value in cell A6 then selected,it also opens the record for customer in cell A6
 
Upvote 0
What would the list index be compared to the actual row number, you can adjust the rw to match the actual row on the sheet.

If listIndex is 7 but the actual row number is 9 then add 2 to the rw.
 
Upvote 0
I think i would be getting further away from fixing it then.
The original post with the code shown works fine for every customer thats selected in the lictbox apart from the customer in A5
Making changes would then put all the others that currently work stop working ?
 
Upvote 0
As Dave said, the listindex starts at 0 so you need to add 1. You should be getting an error for the first item in the listbox as you cannot select A0.
 
Upvote 0
Oops, misread the code. The row number is stored in the listbox in column 4. It appears the wrong row is stored for that item but as that's not done by this code, it's hard to comment.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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