Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Insert VLOOKUP Formula in VBA: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert a VLOOKUP formula with VBA, use the Formula or FormulaR1C1 property of a Range object and assign a string that contains the VLOOKUP function. The shortest working pattern is Range("D2").Formula = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)". This writes a live formula into the cell, just as if you typed it, so it recalculates when the lookup value or the source table changes. You need Excel with macros enabled and a workbook that contains the lookup range; no add-ins or special references are required.

What You'll Need

  • Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels can differ slightly by version.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the VBA code.
  • A lookup table on a worksheet, with the lookup column as the leftmost column of the range you will reference.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on many Macs).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). In the Project Explorer, right-click your workbook and choose Insert > Module. A blank module appears.
  2. Write a Sub procedure. Type Sub InsertVlookup() and press Enter; the editor adds End Sub. All code goes between these lines.
  3. Decide the target cell and the lookup value. For example, if column A holds product IDs and you want the product name in column D, the target is D2 and the lookup value is A2. Use relative references if you plan to fill the formula down.
  4. Build the formula string. The VLOOKUP syntax is VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup]). A working string is "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)". Use FALSE (or 0) for an exact match; TRUE assumes the first column is sorted and can return wrong matches.
  5. Assign it to the cell. Add Range("D2").Formula = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)". The .Formula property expects the formula in A1 style, which is the style you see in the formula bar.
  6. Fill the formula down if needed. Either loop through rows or use Range("D2:D100").Formula = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)". Excel adjusts relative references for each row automatically, so A2 becomes A3, A4 and so on.
  7. Handle sheet names with spaces. If the lookup sheet is called Product List, wrap it in single quotes: "=VLOOKUP(A2,'Product List'!A:B,2,FALSE)". Without quotes the code fails with a runtime error.
  8. Run the macro. Press F5 while the cursor is inside the Sub, or use Run > Run Sub/UserForm. Switch back to Excel to see the formula in the cell and the result in the formula bar.
  9. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A normal .xlsx file discards the module when closed.

Common Mistakes

  • Mixing A1 and R1C1 styles. .Formula uses A1 style; .FormulaR1C1 uses R1C1. Assigning an A1 string to .FormulaR1C1 produces an error or a wrong reference.
  • Forgetting quotes around sheet names with spaces. Always use 'Sheet Name'!A:B.
  • Using a fixed lookup value in a filled range. If you write $A$2 and fill down, every row looks up A2. Use A2 without dollar signs for the lookup value, and absolute references for the table if needed.
  • Leaving out the fourth argument. Omitting FALSE defaults to an approximate match, which can return the nearest smaller value instead of an exact match.
  • Writing the formula in a cell that already contains text. The assignment overwrites the cell. Check the target range first.
  • Using a table array that does not start with the lookup column. VLOOKUP searches the first column of the range, so the key must be leftmost.

Tips

  • Use WorksheetFunction.VLookup when you want the result as a value in VBA rather than a formula in a cell. It returns the matched value and raises an error if nothing is found, so wrap it in error handling.
  • Store the formula string in a variable for readability: Dim f As String: f = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)".
  • Use Range("D2").Formula = f and then Range("D2").AutoFill Destination:=Range("D2:D100") to copy the formula quickly.
  • If the lookup table is on another workbook, include the workbook name in brackets: '[Book2.xlsx]Sheet1'!A:B. Keep that workbook open while the formula recalculates.
  • Test on a copy of the workbook first. A wrong range reference can overwrite data.

Troubleshooting

If the macro runs but the cell shows #N/A, the lookup value is not found in the first column of the table. Check for leading or trailing spaces, numbers stored as text, and whether the table range actually includes the row you expect. If you see #REF!, the column index is larger than the number of columns in the table array. If you see #NAME?, the sheet name is misspelled or lacks quotes. If Excel shows a runtime error 1004, the sheet name probably does not exist or the target cell is on a protected sheet; unprotect it first. If the formula appears as text, the cell was formatted as Text before the assignment; set the number format to General and re-run. If nothing happens at all, check that macros are enabled in the Trust Center and that you are editing the right module.

Frequently Asked Questions

Can I insert a VLOOKUP formula without using .Formula?

Yes. You can use Range("D2").Value = "=VLOOKUP(...)", but .Formula is clearer and avoids type confusion. For a value result inside VBA, use WorksheetFunction.VLookup.

How do I reference a sheet whose name contains a space?

Wrap the sheet name in single quotes inside the formula string: "=VLOOKUP(A2,'My Sheet'!A:B,2,FALSE)".

Why does my filled formula keep looking up the same cell?

You used an absolute reference such as $A$2 for the lookup value. Remove the dollar signs from the lookup value so it changes per row.

Can I use VLOOKUP across workbooks in VBA?

Yes, if the other workbook is open. Include the workbook name in brackets in the table array, for example '[Data.xlsx]Sheet1'!A:B. Closed workbooks may return errors until reopened.

What is the difference between .Formula and .FormulaR1C1?

.Formula uses A1-style references like A2 and B:B. .FormulaR1C1 uses row and column numbers, such as R2C1. Use whichever style you find easier, but do not mix them in one assignment.

Does VLOOKUP work in Excel for Mac VBA?

Yes. The VBA syntax is the same. The keyboard shortcut to open the editor differs, and some older Mac versions handle file paths differently, but the formula string itself is unchanged.