VBA & Macro Problems

Find the Smallest Value That Makes an Excel Test TRUE

Ayan MasoodAyan Masood Nov 12, 2026 869 views

Question details

Find the smallest input value that causes a dependent spreadsheet formula or logical test to return TRUE.

Find the Smallest Excel Value That Makes a Test TRUE
Product
Microsoft Excel VBA and WPS Spreadsheet macros
Device & OS
not provided
Scenario
A single input cell controls a formula or test cell, and the user needs to automate a search for the first successful value.
Observed behavior
The desired result is to leave the smallest value that makes the dependent test evaluate to TRUE in the input cell.
Before you start

Identify the input cell and the dependent test cell before running a macro. Confirm that increasing the input value can eventually make the test return TRUE, and choose a realistic maximum value or iteration limit.

Solution 1Recommended

Use a bounded VBA loop to find the first TRUE result

Use this method when a TRUE/FALSE formula cannot be expressed as a numeric Goal Seek target and one input cell is tested in increasing increments.

The macro starts at a specified value, writes each candidate value to the input cell, recalculates the sheet, and stops at the first TRUE result. A maximum limit prevents the macro from running indefinitely when no matching value exists.

1
Set the input and test cell references

Press Alt+F11 in Excel, select Insert > Module, and replace "A1" with the input cell and "B1" with the cell that contains the TRUE/FALSE test.

2
Add the search macro

Paste this macro into the module: Sub FindSmallestTrue()\n Dim ws As Worksheet\n Dim candidate As Long\n Dim maxValue As Long\n\n Set ws = ActiveSheet\n candidate = 0\n maxValue = 100000\n\n Do While candidate <= maxValue\n ws.Range("A1").Value = candidate\n ws.Calculate\n If ws.Range("B1").Value = True Then\n MsgBox "Smallest value: " & candidate\n Exit Sub\n End If\n candidate = candidate + 1\n Loop\n\n MsgBox "No TRUE result was found up to " & maxValue\nEnd Sub

3
Adjust the starting value and limit

In the VBA editor, change candidate = 0 to the lowest value you want to test and change maxValue = 100000 to the highest permitted value. Change candidate = candidate + 1 if the search must use a different increment.

4
Run the macro

Place the cursor inside FindSmallestTrue and press F5, or return to the workbook and choose Developer > Macros > FindSmallestTrue > Run. The input cell remains set to the first value that returns TRUE.

Use a bounded VBA loop to find the first TRUE result
Use the correct search direction: This loop finds the first successful value only when values are tested from low to high. If the test can switch between TRUE and FALSE multiple times, define the intended range and increment carefully.
WPS Spreadsheet macro tools

Search for the first TRUE value with WPS Spreadsheet

WPS Spreadsheet provides a familiar worksheet environment for building dependent formulas and running compatible macro workflows. You can test incremental values with a bounded macro or use Goal Seek when your logical result can be represented by a numeric helper formula.

  1. 1. Open the workbook in WPS Spreadsheet: Launch WPS Office, select Spreadsheets, and open the Excel workbook containing the input cell and dependent test formula.
  2. 2. Check the logical test: Select the test cell and confirm that its formula returns TRUE or FALSE as the input cell changes.
  3. 3. Add a bounded macro search: Open the macro editor from the Developer tools, insert a module, and add a loop that updates the input cell, recalculates the sheet, and stops when the test is TRUE.
  4. 4. Run and verify the result: Run the macro from the Macros command, then confirm that the value left in the input cell is the first successful value within the selected range.
Use a macro loop to stop at the first input value that makes a logical test TRUE.Use Goal Seek from the Data tools for numeric target formulas.Open and edit Microsoft Excel .xlsx, .xls, and macro-enabled .xlsm workbooks.Work in a lightweight interface with familiar spreadsheet formulas and workbook controls.
microsoft office alternative - wps office

Frequently Asked Questions

Can Excel Goal Seek target a TRUE or FALSE cell directly?

No. Goal Seek expects a numeric target. Create a helper cell such as =--B1 to convert the Boolean result in B1 into 1 for TRUE and 0 for FALSE, then target 1.

How do I make the VBA macro search values other than whole numbers?

Change the increment line from candidate = candidate + 1 to a smaller step, such as candidate = candidate + 0.01, and declare candidate as Double instead of Long.

Why does the macro need a maximum iteration or maximum value?

A limit prevents an endless loop when no tested value makes the formula return TRUE. Set the limit to the highest value that is meaningful for your worksheet.

Will the loop always find the mathematical smallest value?

It finds the first value in the order and increment you specify. For example, a loop starting at 0 and increasing by 1 finds the smallest nonnegative whole-number result, not values between integers.

What should I do if the test cell contains an error instead of TRUE or FALSE?

Correct the formula error first, or add error handling to the macro. Check that cell references, input types, and dependent formulas calculate correctly before running the search.