Determining if Formula Return 1 Cells Contain 'YES' or 'NO'

Abstract: Resolve VBA comparison failures for Excel cells displaying YES or NO by using the .Text property to match visual output regardless of underlying data types.

Created: 2026-09-21 by

To determine if formula cells return 'YES' or 'NO', use the VBA WorksheetFunction to evaluate the formula directly, or check the .Text property of the cell range. This approach bypasses standard value checks that might fail due to calculation states or formatting differences, ensuring accurate detection of boolean-like text outputs in your Excel spreadsheets.

Understanding Formula Output Types

When working with Excel formulas that result in text strings like "YES" or "NO", developers often encounter confusion regarding how VBA interprets these values. It is crucial to distinguish between the actual value returned by the formula and the displayed text. In many cases, a formula might calculate to a logical TRUE or FALSE, which Excel then formats as YES/NO via custom number formatting. However, if the formula explicitly returns the string "YES", the data type is Text, not Boolean. This distinction matters significantly when writing VBA scripts. If you attempt to compare a cell's .Value property against the string "YES", but the cell contains a logical TRUE formatted as YES, the comparison will fail. Conversely, if the formula uses an IF statement like =IF(A1>0,"YES","NO"), the result is strictly a text string. Therefore, the first step in solving this problem is to identify whether the target cells contain formatted logical values or explicit text strings. For scenarios where you need to process large ranges, relying on the visual representation can be more robust than checking underlying values. The .Text property of a Range object returns exactly what the user sees on the screen, including any applied number formats. This makes it an ideal tool for verifying if a cell displays "YES" or "NO", regardless of whether the underlying data is a boolean, a number, or a text string. However, note that .Text reflects the current state of the worksheet; if formulas are set to manual calculation, .Text might show outdated results until recalculated. To ensure accuracy, you should always force a recalculation before reading the text property if there is any doubt about the formula state. This can be achieved using the Calculate method on the specific worksheet or workbook object. By doing so, you guarantee that the displayed text matches the latest computation. This practice is essential in dynamic environments where other macros or events might trigger changes in the spreadsheet simultaneously. If you are dealing with complex logic where simple text matching isn't enough, consider evaluating the formula itself. You can use the Evaluate method in VBA to compute the formula string directly within the script. This allows you to inspect the raw output before any formatting is applied. For example, if the formula is =IF(B2="Active","YES","NO"), evaluating it gives you the direct result. This method is particularly useful when you want to avoid referencing the cell range directly, perhaps because the range is dynamic or hidden. Another consideration is case sensitivity. Excel formulas are generally not case-sensitive, meaning "yes", "Yes", and "YES" are treated identically by the engine. However, when comparing strings in VBA, the default behavior depends on the Option Compare setting. To avoid unexpected mismatches, it is best practice to convert both the cell content and the search string to uppercase or lowercase using the UCase or LCase functions before performing the comparison. This ensures that your script remains robust against minor variations in capitalization that might arise from manual edits or copy-paste operations. For related tasks involving row identification based on values, you might find our guide on identifying zero-value rows helpful, which covers similar patterns for filtering data based on specific criteria. See /news/1270898/identifying-value-0-rows-in-excel for more details on handling value-based conditions.

Implementing the VBA Check

The most straightforward way to check for "YES" or "NO" is to iterate through a range of cells and inspect their properties. Below is a sample subroutine that demonstrates how to loop through a selected range, evaluate each cell, and perform actions based on whether the cell displays "YES" or "NO".
Sub CheckFormulaResults()
    Dim cell As Range
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    
    ' Loop through each cell in the used range of column A
    For Each cell In ws.Range("A1:A10")
        ' Force calculation to ensure up-to-date text
        ws.Calculate
        
        ' Check the .Text property for exact display match
        If UCase(cell.Text) = "YES" Then
            MsgBox "Cell " & cell.Address & " returns YES"
        ElseIf UCase(cell.Text) = "NO" Then
            MsgBox "Cell " & cell.Address & " returns NO"
        Else
            MsgBox "Cell " & cell.Address & " returns something else: " & cell.Text
        End If
    Next cell
End Sub
In this code, we use UCase(cell.Text) to normalize the comparison. We also call ws.Calculate inside the loop, which can be performance-intensive for large ranges. For better efficiency, move the Calculate call outside the loop if possible, ensuring the entire sheet is updated before the inspection begins. This reduces overhead and speeds up execution significantly. If you prefer to evaluate the formula directly rather than relying on the displayed text, you can replace the .Text check with the Evaluate method. This requires constructing the formula string dynamically if you are working with relative references, or simply using the .Formula property of the cell.
Dim formulaResult As Variant
formulaResult = Application.Evaluate(cell.Formula)

If formulaResult = "YES" Then
    ' Action for YES
End If
Note that Evaluate returns a Variant, which may hold different data types depending on the formula's output. Always check the type or convert to string before comparing. This method is safer if you suspect that number formatting might be masking the true result, as it evaluates the mathematical or logical outcome directly. For those converting existing formulas into VBA macros for better performance, understanding the nuances of formula evaluation is key. Our comprehensive guide on converting Excel formulas to VBA macro provides deeper insights into this process. Visit /news/1405093/excel-to-vba-macro-conversion to learn more.

Handling Edge Cases and Errors

When checking formula results, several edge cases can cause errors or incorrect detections. One common issue is error values. If a formula results in #N/A, #VALUE!, or #DIV/0!, the .Text property will display the error symbol (e.g., "#DIV/0!"). Comparing this to "YES" or "NO" will naturally fail, but it’s important to handle these gracefully to avoid runtime errors in your script. You can detect error values by using the IsError function on the cell's value or by checking if the text starts with "#". If an error is detected, you might choose to skip the cell, log the error, or apply a default action. Ignoring errors silently can lead to bugs that are hard to trace, so explicit handling is recommended. Another edge case involves empty cells. If a formula returns an empty string ("") for certain conditions, the .Text property might appear blank. Depending on your requirements, you might want to treat empty cells as "NO" or ignore them entirely. Checking for length or using Trim(cell.Text) can help normalize these inputs. Additionally, consider the impact of merged cells. If your range includes merged cells, the behavior of .Text and .Value can be unpredictable. Only the top-left cell of the merged range typically holds the value, while others may return empty strings. Ensure your range selection accounts for this, or unmerge cells before processing if necessary. Finally, performance is a critical factor when dealing with large datasets. Iterating through thousands of cells one by one in VBA can be slow. In such cases, consider using array operations or advanced filter techniques to process data in bulk. Reading the entire range into a variant array, processing it in memory, and then writing back the results is a much faster approach than looping through individual cells.

Frequently Asked Questions

Why does my VBA code say "YES" when the cell shows "TRUE"?

This happens because Excel often formats logical TRUE/FALSE values as YES/NO using custom number formats. The underlying value is still TRUE, but the display is modified. To fix this, check the .Text property instead of .Value, as .Text reflects the formatted display.

Can I use the Evaluate method to check all cells in a range at once?

No, the Evaluate method works on a single formula or expression at a time. You must loop through each cell or construct a dynamic formula that references the entire range, though looping is generally more reliable for individual cell checks.

How do I handle #N/A errors in my formula checks?

You can use the IsError function to detect if a cell contains an error value. Alternatively, check if the .Text property starts with "#" to identify error displays. Handle these cases separately to prevent your script from failing or misinterpreting the result.

Is checking .Text slower than checking .Value?

Checking .Text is generally slightly slower because it involves formatting calculations, especially if the worksheet has complex conditional formatting or custom number formats. However, for most typical workbooks, the difference is negligible unless you are processing millions of cells.

What if my formula returns a space character instead of empty?

If your formula returns a space (" "), .Text will show a space. Use the Trim function to remove leading and trailing spaces before comparing. For example, use UCase(Trim(cell.Text)) to ensure consistent comparisons.

Tags: Excel Formula VBA

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More