To create a "Like" function in Excel VBA, you use the Like operator within a custom User Defined Function (UDF). This allows you to perform pattern matching—using wildcards like * and ?—directly inside your Excel worksheet cells, similar to how you use the LIKE operator in SQL. While Excel has a built-in SEARCH function, a VBA-based Like function is more powerful because it supports specific character sets and precise pattern requirements.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac PC.
  • Access to the Developer tab (if not visible, it can be enabled via File > Options > Customize Ribbon).
  • A basic understanding of how to enter a formula into a cell.
  • An Excel workbook saved in the .xlsm (Excel Macro-Enabled Workbook) format to ensure the code is preserved.

Step-by-Step

  1. Open the Visual Basic Editor: Launch your Excel workbook. Press Alt + F11 on your keyboard. This keyboard shortcut opens the VBA Editor window, which is the environment where all custom scripts and functions are written.
  2. Insert a New Module: In the VBA Editor, look at the top menu bar. Click on Insert and then select Module. This creates a blank white sheet of code. It is critical to put your function in a standard Module rather than a Sheet or ThisWorkbook object so that the function is available globally across your entire workbook.
  3. Write the Function Code: Copy and paste the following code into the module window. Note that labels and syntax must be exact for the code to execute correctly:
    Function VBA_Like(text_to_check As String, pattern As String) As Boolean
        VBA_Like = text_to_check Like pattern
    End Function
        
    In this code, text_to_check is the cell containing the data you want to evaluate, and pattern is the criteria (including wildcards) you are looking for. The function returns True if the text matches the pattern and False if it does not.
  4. Save the Workbook: Click File > Save As. In the "Save as type" dropdown menu, you must select Excel Macro-Enabled Workbook (*.xlsm). If you save it as a standard .xlsx file, all your VBA code will be permanently deleted when you close the file.
  5. Apply the Function in the Worksheet: Return to your Excel spreadsheet. In any empty cell, type the formula using the name of the function you just created. For example, if your text is in cell A2 and your pattern is in cell B2, type: =VBA_Like(A2, B2).
  6. Test with Wildcards: To verify it works, enter a value like "Apple Pie" in A2. In B2, enter "*Apple*". The formula should return TRUE. If you change B2 to "*Orange*", it should return FALSE.

Common Mistakes

One of the most frequent errors is forgetting to wrap the pattern in quotation marks when typing the formula directly into the cell. If you type =VBA_Like(A2, *Apple*), Excel will throw a formula error because it treats the asterisk as a mathematical operator. You must use =VBA_Like(A2, "*Apple*").

Another common mistake is placing the code in the wrong location. Users often double-click "Sheet1" or "ThisWorkbook" in the Project Explorer and paste the code there. Functions placed in these locations are often private to that specific object and will not appear in the worksheet formula autocomplete list. Always use Insert > Module.

Case sensitivity is also a major point of confusion. By default, VBA's Like operator is case-sensitive. If you search for "*apple*" but the cell contains "Apple", it will return FALSE. To fix this, you can either use Option Compare Text at the very top of your module or wrap your variables in the LCase() function within the code to force everything to lowercase before comparison.

Tips

To make your Like function more versatile, learn the specific VBA wildcard characters. The asterisk (*) represents zero or more characters. The question mark (?) represents exactly one single character. For example, "B?ll" would match "Ball" and "Bell" but not "Billie".

You can also use brackets ([ ]) to match any single character within a specific set. For instance, the pattern "[A-Z]123" will match any single uppercase letter followed by the numbers 123. This is significantly more powerful than the standard SEARCH or FIND functions in Excel.

If you find yourself using the same pattern repeatedly, store the pattern in a named range or a specific cell (e.g., cell $Z$1). This allows you to change the search criteria for thousands of rows instantly by updating a single cell rather than editing every formula.

Troubleshooting

If the function returns a #NAME? error, it means Excel cannot find the function. First, check that you saved the file as an .xlsm. Second, ensure the function is in a standard Module and not a Sheet module. Third, check for typos in the function name; it must match the name defined in the VBA editor exactly.

If the function returns #VALUE!, there is likely a data type mismatch. Ensure that the cells you are referencing contain text. If you are referencing a cell that contains an error (like #N/A), the VBA function will also return an error. You can wrap your formula in =IFERROR(VBA_Like(A2, B2), FALSE) to handle these cases gracefully.

If the function is returning FALSE when you believe it should be TRUE, check for hidden leading or trailing spaces in your data. A cell containing "Apple " (with a space at the end) will not match the pattern "Apple". You can resolve this by modifying the VBA code to VBA_Like = Trim(text_to_check) Like pattern, which removes unnecessary whitespace from the start and end of the text.

Frequently Asked Questions

Is the VBA Like function case-sensitive?

Yes, by default, the Like operator in VBA is case-sensitive. To make it case-insensitive, add Option Compare Text as the very first line of your module, above the function declaration.

Can I use this function in a workbook shared with others?

Yes, as long as the workbook is saved as an .xlsm file and the other users enable macros when they open the file. If macros are disabled, the function will return a #NAME? error.

What is the difference between this and the SEARCH function?

The SEARCH function looks for a substring anywhere within a string. The Like function allows for complex pattern matching, such as requiring a string to start with a specific letter, end with a specific number, or contain only characters from a specific range.

Does this work on Excel for Web?

No, VBA macros and User Defined Functions do not run in the web-based version of Excel. This function will only work in the desktop application for Windows and macOS.

Can I use the Like function to find numbers?

The Like operator is designed for strings. If you are checking a cell containing a number, VBA will automatically try to convert it to a string, but for the best results, ensure your function arguments are defined as String as shown in the step-by-step code.

Can I combine this with other Excel functions?

Absolutely. You can use it inside an IF statement, such as =IF(VBA_Like(A2, "*Test*"), "Valid", "Invalid"), to categorize data based on the pattern match results.