简体   繁体   中英

VBA VLookUp - Not working but no Error

Thanks for casting your eyes! I'm afraid I'm completely new to VBA and am struggling a little I have to admit.

I'm trying my first project, learning by doing, and I've run into my first roadblock. No Errors, but my VLookUp doesn't want to return any data.

Basically I'm trying to select all data in my "Input" tab, cut it and past it to my "Output" Tab, then do a VLookUp from my "Client List" tab to return values to output.

My code looks like this;

Option Explicit
Sub RP_Maker()
''''''variables

'Misc

Dim x As Long

'Worksheets

Dim ws_Input As Worksheet
Set ws_Input = Workbooks("RP Maker").Worksheets("Input")

Dim ws_Output As Worksheet
Set ws_Output = Workbooks("RP Maker").Worksheets("Output")

Dim ws_ClientList As Worksheet
Set ws_ClientList = Workbooks("RP Maker").Worksheets("Client List")

Dim ws_PRP As Worksheet
Set ws_PRP = Workbooks("RP Maker").Worksheets("Previous RPs")

'Last Column & Row

Dim lCol As Long
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Dim lRow As Long
lRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

'''''Code

'Cut Input sheet and paste to Output sheet.

ws_Input.Activate

ws_Input.Range(Cells(1, 1), Cells(lRow, lCol)).Cut ws_Output.Cells(1, 1)

ws_Output.Activate

'VLookUp

ws_ClientList.Activate

For x = 2 To lRow

    On Error Resume Next
     ws_Output.Cells(x, 4) = Application.WorksheetFunction.VLookup(ws_Output.Cells(x, 1), ws_ClientList.Range(Cells(1, 1), Cells(lRow, lCol)), 4, False)
    On Error GoTo 0

 Next x

 ws_Output.Activate


 End Sub

It doesn't throw up any errors, but im guessing that's because I've used (probably incorrectly), the error handler.

There is relevant data there to be looked up so each run should not be an error. About half are blank and half have data.

Thanks all!

Callum

The following is trying to construct a range using cells from another worksheet (the default ActiveSheet property ).

 ws_ClientList.Range(Cells(1, 1), Cells(lRow, lCol))

It should be closer to,

 ws_ClientList.Range(ws_ClientList.Cells(1, 1), ws_ClientList.Cells(lRow, lCol))

There are a couple of places where you repeat this malformed range referencing but you are getting away with it due to the correct ActiveSheet. Better to explicitly reference all Range object and Range.Cells property Range.Parent properties.

Thank you everybody for all your help. It was the poor cell/range referencing on my part which was to blame.

I've amended as suggested and it works perfectly, just incase anyone else is suffering similarly, this is what my working code looks like;

'VLookUp

For x = 2 To lRow

On Error Resume Next
ws_Output.Cells(x, 4) = Application.WorksheetFunction.VLookup(ws_Output.Cells(x, 1), ws_ClientList.Range(ws_ClientList.Cells(1, 1), ws_ClientList.Cells(lRowClientlist, lColClientList)), 4, False)
On Error GoTo 0

Next x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM