VBA Lookup using Tables

Skybluekid

Well-known Member
Joined
Apr 17, 2012
Messages
1,231
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am using the code below to lookup a value from a Table.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim LU As String
Dim Auth As Variant
Dim LU2 As String
Dim Tbl As ListObject
Dim Rng As Variant
Dim Rng2 As Variant
Dim FT As Variant

Set Tbl = Sheets("Extra").ListObjects("AuthorityTable")
Set Rng = Tbl.ListColumns("Fining Authority")
Set Rng2 = Tbl.ListColumns("Fine Type")

'Insert Authority
If Not Intersect(Target, Range("C:C")) Is Nothing Then
LU = Left(Target.Value, 2)
Auth = Application.VLookup(LU, [AuthorityTable], 2, 0)
If IsError(Auth) Then Auth = "Choose from List"
Target.Offset(, 4).Value = Auth
End If




'Insert Fine Type
If Not Intersect(Target, Range("C:C")) Is Nothing Then
LU2 = Target.Offset(, 4).Value
FT = Application.WorksheetFunction.Index(Sheets("Extra").Range("Rng"), Application.WorksheetFunction.Match(LU2, Sheets("Extra").Range("Rng2"), 0))
If IsError(FT) Then FT = "Choose From List"
Target.Offset(, 8) = FT
End If


End Sub

The code crashes on this line

FT = Application.WorksheetFunction.Index(Sheets("Extra").Range("Rng"), Application.WorksheetFunction.Match(LU2, Sheets("Extra").Range("Rng2"), 0))


saying its an Application or Object defined error.


What the code does is to Look at the first to characters of the Target, then look up the Authority, then it will use that Authority to return a Fine Type, from the same table. The 1st Lookup uses Columns 1 and 2 of the table, where as the send lookup uses columns 2 and 3, hence using Index & Match rather than Vlookup.

I am little stumped.

Thanks in Advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try
Code:
FT = Application.Index(Rng, Application.match(LU2, Rng2, 0))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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