Forum Discussion
Macro to vlookup from a list if specific text is met
I would like to create a macro to vlookup from a list if specific text is met. However, when i execute the macro, the error msg was popped up: "Run-time error '1004' Application or Object-defined error".
this is the code below, not sure whether if the code is correct..
Application.ScreenUpdating = FALSE
Dim ws As Worksheet
Dim Destwb As Workbook
Dim Destws As Worksheet
Dim DC2_startrow As Long
Dim DC2_lastrow As Long
Dim wsrng As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("TCA")
Set Destwb = Workbooks.Open("C:\Users\hrhquek\Desktop\DC consol testing.xlsx")
Set Destws = Destwb.Sheets("Rapid - List With DC")
DC2_startrow = ws.Range("C:C").Find(What:="DC2", After:=ws.Range("C1")).Row
DC2_lastrow = ws.Range("C:C").Find(What:="DC2", After:=ws.Range("C1"), SearchDirection:=xlPrevious).Row
Set wsrng = ws.Range("D" & DC2_startrow & ":K" & DC2_lastrow)
Destws.Range("V1").EntireColumn.Insert
'On Error Resume Next
For i = DC2_startrow To DC2_lastrow
Destws.Range("V" & i).Value = Application.WorksheetFunction.VLookup(Destws.Range("A" & i).Value, wsrng, 1, 0)
Next i
Application.ScreenUpdating = TRUE
End Sub
Attaching a pic of how my source workbook (containing macro) would look.
Appreciate the assistance provided in advance!
Thank you. As I mentioned, DC2_startrow and DC2_lastrow are row numbers on the source sheet.
You use them as row numbers on the destination sheet. That makes no sense.
Can you explain in words what you're trying to accomplish with this macro (which belongs in a standard module, by the way, not in a worksheet module)?
7 Replies
You look through cells on Destws, but the loop bounds DC2_startrow and DC2_endrow are calculated based on ws. Do the sheet match exactly?
And you specify 1 as column index in VLookup, so it should return the search value.
As usual, it is impossible to know what goes wrong without seeing the workbooks.
- hrh_dashIron Contributor
Hi HansVogelaar , there will be 2 workbooks. Source and Dest.
Source wb will be the file containing the macro.
i tried to insert an IF ELSE function but it doesn't solve the issue. I only need to vlookup based on values that are DC2.
Attaching both files for your reference.
Thank you. As I mentioned, DC2_startrow and DC2_lastrow are row numbers on the source sheet.
You use them as row numbers on the destination sheet. That makes no sense.
Can you explain in words what you're trying to accomplish with this macro (which belongs in a standard module, by the way, not in a worksheet module)?