Nipen Mahajan
Nipen Mahajan

Reputation: 27

VLOOKUP not applying to all the rows through VBA

I am applying VLOOKUP to one column using 2 sheets. The code works but it is applied only in one cell. I want to apply to all the cells below header. Below is my code which I am trying to execute:

ESheet.Range("C2").Formula = "=VLOOKUP(B2, Roles!$A:$B, 2, FALSE)"

Upvotes: 1

Views: 172

Answers (1)

FaneDuru
FaneDuru

Reputation: 42236

Use this code, please:

Sub VlookupAlColumn()
 Dim ESheet As Worksheet, lastR As Long
 'Set ESheet = ActiveSheet 'only for me, for testing reason
 lastR = ESheet.Range("C" & Cells.Rows.Count).End(xlUp).row
 ESheet.Range("C2:C" & lastR).Formula = "=VLOOKUP(B2, Roles!$A:$B, 2, FALSE)"
End Sub

Upvotes: 3

Related Questions