Forum

How to generate who...
 
Notifications
Clear all

How to generate whole table of values using code for VLookUp?

5 Posts
2 Users
0 Reactions
111 Views
(@step10)
Posts: 6
Active Member
Topic starter
 

Hi!

Basically I'm trying to do something similar to that of Example 3 given in the below link:  https://www.exceltrick.com/formulas_macros/vlookup-using-dates-in-vba/

The only difference is that the table (equivalent to 'Employee Table 1') I'm comparing to is on another worksheet. 

I used the code given in the link above but modified the code slightly to suit my purposes. 

Here is my code:

Sub Classify()

''copies over Product Type from hidden 'Product Codes' tab

''uses VLookup function

On Error Resume Next

Dim Pdt_Row As Long

Dim Pdt_Clm As Long

Sheets("P1").Select

finalrow = Cells(rows.Count, "A").End(xlUp).Row

Table1 = Worksheets("P1").Range("A14:A" & finalrow) ''Product Codes Column

Table2 = Worksheets("Product Codes").Range("A11:J397") ''Range of Product Types

Pdt_Row = Worksheets("P1").Range("E14").Row 

Pdt_Clm = Worksheets("P1").Range("E14").Column

For Each Cell In Table1

Worksheets("P1").Cells(Pdt_Row, Pdt_Clm) = Application.WorksheetFunction.VLookup(Cell, Table2, 10, False)

Pdt_Row = Pdt_Row + 1

Next Cell

End Sub

However, everytime I run the code, it will stop after a few lines. For example, say there are 100 product codes. The code will run for the first 20 product codes and identify the product name correctly for the first 20 codes. However, it stops there and does not continue.

I can't figure out what's the problem. 

Would appreciate some help here, thanks so much!

 
Posted : 20/07/2018 4:20 am
(@step10)
Posts: 6
Active Member
Topic starter
 

Hi Sunny,

Pls refer to this sample worksheet.

Basically, when I click on the button 'Identify Product Type' in the 'Data' tab, I want to identify the Product Type each product code is linked to. This information can then be obtained from the 'Pdt Codes' tab.

However when I run the macro 'Classify', it will only do so up to B48. From B49 onwards, the code does not work.

How do I solve this problem?

Thanks!

 
Posted : 20/07/2018 10:17 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Stephanie

The VLOOKUP is unable to locate the codes 1010 to 1050 from your Pdt Codes sheet.

This is because these codes in the Data sheets are TEXT while those in the Pdt Codes sheet are NUMBERS.

Just change these codes in Pdt Codes to TEXT by putting a single quote in front of them like what you did for 0010 to 0997.

 

Use On Error Resume Next with care as it will ignore errors (like the VLOOKUP error you encountered) and continue to process.

Just try removing/commenting this line and run your code on your original file and you will see what I mean.

Hope this helps.

Sunny

 
Posted : 21/07/2018 1:42 am
(@step10)
Posts: 6
Active Member
Topic starter
 

Hi Sunny,

That works. 

Thank you!

 
Posted : 24/07/2018 10:18 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Thanks for your feedback.

Glad to know it is working for you.

Sunny

 
Posted : 25/07/2018 3:10 am
Share: