Vlookup VBA Help

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
128
Can someone help me update my code to not bug when doing the VLOOKUP if there is only 1 row of data on the sheet? It has no issues if there is no data or if there is more than 1 row of data, but I don't know why it bugs with only 1 row.

VBA Code:
Sheets("Summer Audit").Select
                
    Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-12],'Load Status'!C[-12],1,FALSE)"
    Range("O2").AutoFill Destination:=Range("O2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Range("P2").FormulaR1C1 = "=VLOOKUP(RC[-13],'Not Taken'!C[-13],1,FALSE)"
    Range("P2").AutoFill Destination:=Range("P2:P" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Range("Q2").FormulaR1C1 = "=VLOOKUP(RC[-14],'Zero Earnings'!C[-14],1,FALSE)"
    Range("Q2").AutoFill Destination:=Range("Q2:Q" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Range("R2").FormulaR1C1 = "=VLOOKUP(RC[-15],'Not Processed'!C[-15],1,FALSE)"
    Range("R2").AutoFill Destination:=Range("R2:R" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Range("S2").FormulaR1C1 = "=VLOOKUP(RC[-16],'Mismatch'!C[-16],1,FALSE)"
    Range("S2").AutoFill Destination:=Range("S2:S" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Range("T2").FormulaR1C1 = "=VLOOKUP(RC[-17],'Deduction'!C[-17],1,FALSE)"
    Range("T2").AutoFill Destination:=Range("T2:T" & Cells(Rows.Count, "A").End(xlUp).Row)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,671
Office Version
  1. 365
Platform
  1. Windows
Try it like
VBA Code:
   Dim Usdrws As Long
   Sheets("Summer Audit").Select
   Usdrws = Range("A" & Rows.Count).End(xlUp).Row
   If Usdrws >= 2 Then
      Range("O2:O" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-12],'Load Status'!C[-12],1,FALSE)"
      Range("P2:P" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-13],'Not Taken'!C[-13],1,FALSE)"
      Range("Q2:Q" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-14],'Zero Earnings'!C[-14],1,FALSE)"
      Range("R2:R" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-15],'Not Processed'!C[-15],1,FALSE)"
      Range("S2:S" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-16],'Mismatch'!C[-16],1,FALSE)"
      Range("T2:T" & Usdrws).FormulaR1C1 = "=VLOOKUP(RC[-17],'Deduction'!C[-17],1,FALSE)"
   End With
 

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
128
I just changed the End With to End If :) otherwise it seems to work great! Is this a short/condensed/cleaned up version that I can update all my vlookups to on the other sheets I have?
 

Watch MrExcel Video

Forum statistics

Threads
1,129,816
Messages
5,638,496
Members
417,029
Latest member
lingx86

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
Top