I have a workbook that collates records on different clients. One worksheet holds all records of all clients (the datasheet). I want to have another worksheet (the client sheet) within the workbook that pulls all distinctive records of a single client (which will be in a data validated input cell on that sheet) and loads them into a table (which starts at row 5). For each client, there will be a different number of records to return from the datasheet. I want there to be a sort of VBA 'Load' button that will do all the heavy lifting so you only need to select which client you want to see in the input cell.
My code works, however when the ID column is sorted in the datasheet large to small (as it is easier to input new records at the top of the list) I get an error. Additionally, the records that do manage to load, are incorrect and duplicated. How can I sort the datasheet ID column large to small without running into issues with my VLookup?
Sub Load_Client_Records()
Dim Row As Long
Dim Key As Long
Dim VlookupResult As Variant
Row = 5
Key = 1
For Each Client In Sheets("datasheet").Range("Master_IDs")
VlookupResult = WorksheetFunction.VLookup(Client, Sheets("datasheet").Range("RecordTable"), 2)
If (Sheets("client sheet").Range("Client_Name") = VlookupResult) Then
Cells(Row, 1).Value = Key
Cells(Row, 2).Value = VlookupResult
Row = Row + 1
Key = Key + 1
End If
Next
End Sub
When running with the datasheet ID column sorted large to small, I get the error: "Run-time error '1004' Unable to get Lookup Property of the Worksheet Function class"