Smart Systems for Home

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

Excel VBA VLOOKUP Macro for Classes Sheet

· 4 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 create a VBA VLOOKUP macro for a classes sheet, you need a lookup table (e.g., class codes and descriptions) and a data sheet where you want to pull matching values. The macro uses Application.WorksheetFunction.VLookup to search the first column of the lookup range and return a value from a specified column. This guide walks you through writing and running the macro, handling errors, and avoiding common pitfalls. No add-ins are required—just Excel with macros enabled.

What You'll Need

  • Excel desktop (Windows or Mac) with macro-enabled workbook (.xlsm). VBA is not available in Excel for the web.
  • A sheet named Classes containing your lookup table (e.g., class code in column A, class name in column B, instructor in column C).
  • A data sheet (e.g., Data) where you want the VLOOKUP results to appear.
  • Basic familiarity with the VBA editor (ALT+F11 on Windows, FN+ALT+F11 on Mac).

Step-by-Step

  1. Prepare your sheets. In the Classes sheet, ensure the first column contains unique lookup values (e.g., class codes like "MATH101"). In the Data sheet, have the lookup values in a column (e.g., column A) and leave adjacent columns empty for the returned values.
  2. Open the VBA editor. Press ALT+F11 (Windows) or FN+ALT+F11 (Mac). In the Project Explorer, right-click your workbook and choose Insert > Module.
  3. Write the macro. Paste the following code into the module, adjusting sheet names and ranges as needed:
    Sub VLookupClasses()
        Dim wsData As Worksheet, wsClasses As Worksheet
        Dim lastRow As Long, i As Long
        Dim lookupRange As Range
        Set wsData = ThisWorkbook.Sheets("Data")
        Set wsClasses = ThisWorkbook.Sheets("Classes")
        Set lookupRange = wsClasses.Range("A2:C100") ' adjust to your table
        lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastRow
            On Error Resume Next
            wsData.Cells(i, 2).Value = Application.WorksheetFunction.VLookup(wsData.Cells(i, 1).Value, lookupRange, 2, False)
            If Err.Number <> 0 Then
                wsData.Cells(i, 2).Value = "Not found"
                Err.Clear
            End If
            On Error GoTo 0
        Next i
    End Sub
  4. Adjust the ranges. Change "A2:C100" to cover your actual lookup table. The third argument in VLookup (here 2) is the column index within the lookup range—use 2 for the second column, 3 for the third, etc. The last argument False forces an exact match.
  5. Run the macro. Close the VBA editor. Press ALT+F8, select VLookupClasses, and click Run. Results appear in column B of the Data sheet.
  6. Save as macro-enabled. Save the workbook as Excel Macro-Enabled Workbook (*.xlsm) to keep the code.

Common Mistakes

  • Wrong column index: The third argument counts from the first column of the lookup range, not the sheet. If your range starts at column B, column C is index 2.
  • Unsorted data with approximate match: Using True or omitting the fourth argument can return wrong results if the first column isn't sorted. Always use False for exact matches.
  • Sheet name mismatch: The code refers to "Data" and "Classes". If your sheets have different names (including spaces), update the strings or use the sheet's CodeName.
  • Not handling errors: Without On Error Resume Next, a missing lookup value stops the macro. The example above handles it, but you can customize the fallback text.
  • Lookup value type mismatch: VLOOKUP treats text and numbers differently. Ensure the lookup value in Data matches the data type in Classes (e.g., both text or both numbers).

Tips

  • Use a dynamic range like wsClasses.Range("A2").CurrentRegion to automatically include all contiguous data.
  • To return multiple columns, loop through column indexes or use INDEX/MATCH in VBA for more flexibility.
  • Add a header check or a message box to confirm the macro ran successfully.
  • Test on a copy of your workbook before running on live data.
  • For large datasets, disable screen updating (Application.ScreenUpdating = False) and re-enable at the end to speed up execution.

Troubleshooting

If the macro doesn't run, ensure macros are enabled: go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros (not recommended for untrusted files) or Disable all macros with notification and click Enable Content when prompted. If you get a "Subscript out of range" error, a sheet name is misspelled. If results are blank, check that the lookup range includes the return column and that the lookup values exist. If the macro is slow, limit the range to the actual data area. For #N/A errors, verify exact match and data types. On Mac, some WorksheetFunction methods may behave differently; test with a small dataset first.

Frequently Asked Questions

Can I use VLOOKUP in VBA without a macro-enabled workbook?

No, VBA requires an .xlsm or .xlsb file. Saving as .xlsx removes the macro.

How do I return a value from a column to the left of the lookup column?

VLOOKUP cannot look left. Use INDEX and MATCH instead, e.g., Application.WorksheetFunction.Index(range, Application.WorksheetFunction.Match(...)).

Why does my macro return #N/A for values that exist?

Common causes: trailing spaces, number stored as text, or the lookup range not including the return column. Use TRIM or check cell formatting.

Can I run this macro on Excel for the web?

No, Excel for the web does not support VBA macros. Use the desktop app.

How do I make the macro case-insensitive?

VLOOKUP is case-insensitive by default. If you need case-sensitive matching, use EXACT with array formulas or a custom VBA function.

What if my lookup table grows?

Use a dynamic named range or CurrentRegion so the macro automatically includes new rows.