VBA & Macro Problems

Convert Excel IF Formulas to a VBA Function

Camila MilosovichCamila Milosovich Nov 22, 2026 868 views

Question details

Create a reusable function that converts text responses into numeric codes instead of maintaining nested IF formulas.

Convert Excel IF Formulas to a VBA Function
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains responses such as Yes, No, and Unknown that need consistent numeric codes across a range.
Observed behavior
A reusable worksheet function or lookup table should return the appropriate code for each response.
Before you start

Confirm the response-to-code mappings and identify the column containing the responses. To use a VBA function, open the workbook in a desktop version of Excel that supports VBA.

Solution 1Recommended

Create a VBA Function for Response Codes

Use one worksheet function when the response mappings are stable and you want to fill the function down a column.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the editor, select your workbook and choose Insert > Module.

2
Add the function

Paste this code into the new module: Function ResponseCode(value As Variant) As Variant ResponseCode = "" If IsError(value) Then Exit Function Select Case LCase(Trim(CStr(value))) Case "no": ResponseCode = 0 Case "yes": ResponseCode = 1 Case "not applicable": ResponseCode = 7 Case "refused to answer": ResponseCode = 8 Case "unknown": ResponseCode = 9 End Select End Function

3
Use the function in the worksheet

Return to the worksheet and enter =ResponseCode(A2) in the output cell next to the response in A2. Drag the cell's fill handle down to apply it to the remaining rows.

4
Save the function

Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). When reopening the file, enable macros only if you trust the workbook.

Create a VBA Function for Response Codes
Unmatched responses: The function ignores capitalization and surrounding spaces. Unrecognized responses and error values return an empty string.
Convert responses with a lookup table

Map Text Responses to Codes in WPS Spreadsheets

Use a lookup table and VLOOKUP in WPS Spreadsheets to assign numeric codes without writing VBA. Update the table when your mappings change.

  1. 1. Open the workbook: Open WPS Spreadsheets and select Open to load the workbook containing your responses.
  2. 2. Enter the mappings: In E2:F6, enter No/0, Yes/1, Not Applicable/7, Refused to Answer/8, and Unknown/9, with the response in column E and its code in column F.
  3. 3. Add the lookup formula: Beside the first response in A2, enter =IFERROR(VLOOKUP(A2,$E$2:$F$6,2,FALSE),"") and press Enter.
  4. 4. Fill and save: Drag the formula cell's fill handle down the response list, then choose File > Save As to save the workbook in Excel XLSX format.
Convert repeated text responses into consistent numeric codesMaintain mappings in worksheet cells rather than VBA codeOpen and save Microsoft Excel XLSX workbooksWork with a familiar spreadsheet interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does the VBA function return a blank cell?

The function returns an empty string for unrecognized text or an error value. Check the response spelling against the Case entries in the VBA module.

How do I change or add a response code?

In the VBA editor, open the module and edit or add a Case line. If you use a lookup table instead, update the relevant cells and expand the formula's lookup range if needed.

Why can't Excel find my custom function?

Check that the function is in a standard module under Insert > Module, that the workbook is saved as XLSM, and that macros are enabled for a trusted workbook.

Can I convert the responses without enabling macros?

Yes. Enter the response and code pairs in a worksheet table and use =IFERROR(VLOOKUP(A2,$E$2:$F$6,2,FALSE),"") to retrieve the code.