Help with vlook in listbox

RAires

New Member
Joined
Aug 31, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I have a listbox displaying the table in my form so you can see the changes you make there instead of having to go to the excel sheet.
I have the userform populate the boxes when i select a row in the listbox, the problem im running into is that some columns are not filled and that gives me a vlook error. is there a way to make it appear as blank when the cell is not filled?
this is the code i have, there 30 more rows but the code is the same

VBA Code:
Private Sub lstDisplay_AfterUpdate()


Me.txtdepot.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 1, False)
Me.txtref_clients.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 2, False)
Me.txtproprietaire.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 3, False)
Me.txtadress.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 4, False)
Me.txtawb.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 5, False)
Me.txtmerchandise_sensible.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 8, False)
Me.txtdate_entreposage.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 10, False)
Me.txtentrepositaire_initial.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 11, False)
Me.txtrefclasseur.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 12, False)
Me.txtdouane_emetrice.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 13, False)
Me.txttransitaire.Value = Application.WorksheetFunction.VLookup(lstDisplay.Value, Range("B6:CV4985"), 14, False)
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
One option would be
VBA Code:
   Dim x As Variant
   x = Application.VLookup(lstDisplay.Value, Range("B6:CV4985"), 1, False)
   If Not IsError(x) Then Me.txtdepot.Value = x
   x = Application.VLookup(lstDisplay.Value, Range("B6:CV4985"), 2, False)
   If Not IsError(x) Then Me.txtref_clients.Value = x
 
Upvote 0
Another option would be like
VBA Code:
   Dim x As Variant
   
   x = Application.Match(lstDisplay.Value, Range("B6:B4985"), 0)
   Me.txtdepot.Value = Cells(x, 2).Value
   Me.txtref_clients.Value = Cells(x, 3).Value
That way you only do the lookup once.
 
Upvote 0
How are you populating the listbox?

If you are populating it from a table the the ListIndex of the selected row in the listbox should correspond directly to the row in the table so you wouldn't need formulas to get the values for the textboxes.
 
Upvote 0
@Fluff its working perfectly now! thank you very much

@Norie not all columns have data for all items, i think that's why i was having trouble with the code i was using.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,513
Messages
6,125,262
Members
449,219
Latest member
daynle

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