Convert Excel IF Formulas to a VBA Function
Question details
Create a reusable function that converts text responses into numeric codes instead of maintaining nested IF formulas.

- 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.
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.
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.
In Excel, open the Developer tab and select Visual Basic. In the editor, select your workbook and choose Insert > Module.
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
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.
Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). When reopening the file, enable macros only if you trust the workbook.

Use a Lookup Table Instead of VBA
Use a lookup table when mappings may change or you prefer not to use macros.
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. Open the workbook: Open WPS Spreadsheets and select Open to load the workbook containing your responses.
- 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. Add the lookup formula: Beside the first response in A2, enter =IFERROR(VLOOKUP(A2,$E$2:$F$6,2,FALSE),"") and press Enter.
- 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.

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.




