Using VLookup in VBA - Help please

tommill52

New Member
Joined
May 12, 2015
Messages
27
Hi, VBA novice here.

I have two worksheets (T2 and PalletQty). The data in T2 is generated from a number of other worksheets in a grid format - all this works OK. The number of columns is known but the number of rows will vary. A number of operations are carried out on the rows, starting at row 3, iteratively, to the last row. All but the VLookup function (the last line) work OK.

The objective is, working on the current row specified by variable 'iRow', to look up the part in column A of 'T2', find that part in column 'A' of worksheet 'PalletQty', read the value in column B of 'PalletQty', and store it in column AG of worksheet 'T2'.

The coding is as follows (sorry, I couldn't see a box for loading code into so I have embedded it here).

Many thanks

Code:
With Application.WorksheetFunction

Sheets("T2").Activate

For iRow = 3 To LastRow + 1
     
     Worksheets("T2").Cells(iRow, "M").FormulaR1C1 = "=SUM(RC[1]:RC[6])"   
     Worksheets("T2").Cells(iRow, "AI").FormulaR1C1 = "=SUM(RC[-26]:RC[-23])"
     Worksheets("T2").Cells(iRow, "AJ").FormulaR1C1 = "=(SUM(RC[-27]:RC[-24]))+RC[-21]"

     Worksheets("T2").Range("AG" & iRow) "=VLOOKUP(A2,PalletQty!$A$2:$C$150,2,False)"
         
     Next iRow
  
  End With
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Worksheets("T2").Range("AG" & iRow).FormulaR1C1 = "=VLOOKUP(A2,PalletQty!$A$2:$C$150,2,False)"

That didn't work?
 
Upvote 0
Try this...

Code:
Worksheets("T2").Range("AG" & iRow) = "=VLOOKUP(A[B]" & iRow & "[/B],PalletQty!$A$2:$C$150,2,False)"

Or this to replace the loop

Code:
    Sheets("T2").Activate
    
    [color=darkblue]With[/color] Rows("3:" & LastRow + 1)
     
     .Columns("M").FormulaR1C1 = "=SUM(RC[1]:RC[6])"
     .Columns("AI").FormulaR1C1 = "=SUM(RC[-26]:RC[-23])"
     .Columns("AJ").FormulaR1C1 = "=(SUM(RC[-27]:RC[-24]))+RC[-21]"
    
     .Columns("AG").Formula = "=VLOOKUP(A3,PalletQty!$A$2:$C$150,2,False)"
        
  [color=darkblue]End[/color] [color=darkblue]With[/color]
 
Last edited:
Upvote 0
Hi, I don't know why your code is showing as deleted - I don't think I did anything! Fortunately, I copied the one liner and it worked. Thank you very much for your help. Regards.
 
Upvote 0
Hi, I don't know why your code is showing as deleted - I don't think I did anything! Fortunately, I copied the one liner and it worked. Thank you very much for your help. Regards.

You're welcome.

I accidentally posted twice and then deleted the 2nd one.
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,844
Members
449,411
Latest member
adunn_23

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