Smart Systems for Home

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

Excel VBA: Convert String to Boolean

· 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 convert a string to a Boolean in Excel VBA, use the CBool function on strings that contain "True" or "False" (or numeric strings like "0" and "1"). For other text such as "Yes"/"No" or "Y"/"N", write a small helper function that maps the accepted words to True or False. VBA does not have a built-in parser for arbitrary text, so you must decide which strings count as true and handle everything else explicitly.

What You'll Need

  • Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
  • Access to the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on Mac).
  • A workbook saved as .xlsm if you want to keep macros.
  • Basic familiarity with inserting a module and running a macro.

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. Note that menu labels can differ slightly by version, device or region.
  2. Test the built-in conversion. In the new module, type a short procedure:
    Sub TestCBool()
        Dim s As String
        s = "True"
        MsgBox CBool(s)
    End Sub
    Press F5 to run it. A message box should show True. CBool also accepts "False" and numeric strings like "0" (False) and "1" (True). Any other text raises a type mismatch error.
  3. Decide your accepted values. List the strings your data might contain, for example "True", "False", "Yes", "No", "Y", "N", "1", "0". Write them down before coding so the helper function is predictable.
  4. Create a helper function. Add this function to the module:
    Function StringToBoolean(ByVal s As String) As Boolean
        Select Case LCase(Trim(s))
            Case "true", "yes", "y", "1"
                StringToBoolean = True
            Case "false", "no", "n", "0"
                StringToBoolean = False
            Case Else
                Err.Raise vbObjectError + 1, "StringToBoolean", "Unrecognized value: " & s
        End Select
    End Function
    The LCase and Trim calls make the comparison case-insensitive and ignore leading or trailing spaces.
  5. Call the function from a procedure. For example:
    Sub Demo()
        Dim result As Boolean
        result = StringToBoolean(" Yes ")
        MsgBox result
    End Sub
    Running this shows True. If you pass an unlisted value such as "Maybe", the function raises a clear error instead of silently returning False.
  6. Handle blanks and empty strings. Decide whether an empty cell should be False, True or an error. A common choice is to treat blank as False by adding Case "" to the False branch, or to skip blank rows in your loop. Document the choice in a comment so future readers know.
  7. Use the function on worksheet data. Loop through a range and write results to another column:
    Sub ConvertRange()
        Dim c As Range
        For Each c In Range("A2:A10")
            If Len(Trim(c.Value)) > 0 Then
                c.Offset(0, 1).Value = StringToBoolean(CStr(c.Value))
            End If
        Next c
    End Sub
    This reads each cell in A2:A10 and writes the Boolean to the adjacent cell in column B.
  8. Save and test with edge cases. Try values with extra spaces, mixed case ("tRuE"), numeric strings, and an unexpected word. Confirm the error message appears for the unexpected word and that valid values convert correctly. Save the workbook as .xlsm.

Common Mistakes

  • Assuming CBool handles "Yes" or "No". It does not; those raise a type mismatch. Use the helper function for any non-standard text.
  • Forgetting to trim spaces. " True" with a leading space fails in CBool and in a naive comparison. Always Trim first.
  • Ignoring case. VBA string comparison is case-sensitive by default in Select Case unless you use LCase or Option Compare Text. Use LCase for clarity.
  • Silently defaulting to False. A function that returns False for every unrecognized string hides data-entry errors. Raise an error or log the bad value instead.
  • Passing non-string values. If a cell contains an error value such as #N/A, CStr may fail. Check for errors with IsError before converting.
  • Using Boolean in a worksheet formula context. VBA Booleans written to cells appear as TRUE/FALSE, which is fine, but comparing them to text later needs care.

Tips

  • Keep the list of accepted true and false words in one place (the helper function) so you can update it easily.
  • Use Option Explicit at the top of the module to catch undeclared variables.
  • If your data comes from an external system, check its documentation for the exact strings it exports before writing the mapping.
  • For large ranges, read values into a Variant array and process in memory rather than writing cell by cell; this is much faster.
  • Add a comment above the function explaining the accepted values and the blank-handling rule.

Troubleshooting

  • Run-time error 13: Type mismatch. The string is not "True", "False" or a numeric string. Use the helper function or clean the data first.
  • The function returns False for everything. Check that you are not comparing against the wrong case or that the cell values contain hidden spaces or non-breaking spaces. Use Trim and inspect with Len.
  • Error raised for a value you expected to work. Confirm the exact spelling in your Select Case list; add the missing variant or correct the source data.
  • Macro does nothing. Ensure macros are enabled and the code is in a standard module, not a sheet module, unless you intend it to be there.
  • Boolean written to cell shows as text. If the destination cell is formatted as Text, change it to General before writing.

Frequently Asked Questions

Can CBool convert any string to a Boolean?

No. CBool accepts "True", "False" and numeric strings such as "0" and "1". Other words like "Yes" cause a type mismatch.

How do I treat blank cells as False?

Add Case "" to the False branch of the helper function, or check Len(Trim(c.Value)) = 0 before calling it and assign False directly.

Is VBA string comparison case-sensitive?

By default yes. Use LCase or UCase on both sides, or place Option Compare Text at the top of the module to make comparisons case-insensitive.

What happens if the string contains extra spaces?

CBool fails and a direct comparison fails too. Always apply Trim before converting or comparing.

Can I use this function in a worksheet formula?

Only if you save the workbook as .xlsm and call it as a user-defined function, for example =StringToBoolean(A1). The function must be in a standard module and public.

How do I handle numeric strings like "-1"?

CBool treats any nonzero number as True and zero as False. If you want only "0" and "1" accepted, use the helper function instead of CBool.