How to Run Goal Seek Across Multiple Rows in Excel Using VBA
Question details
The user wants to automate the Goal Seek function across multiple rows in Excel to calculate values like selling price based on target gross margins.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating repetitive Goal Seek calculations across large datasets without performing them manually row by row.
- Observed behavior
- Needs a VBA code solution to loop through a range of target cells and apply the Goal Seek function dynamically to corresponding changing cells.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab in your spreadsheet application to write and run VBA code.
Use a VBA Macro to Loop Goal Seek Through Specific Rows
Create a VBA subroutine that utilizes a For Each loop to iterate through your target cells, applying Goal Seek to each row automatically.
By default, Excel's built-in Goal Seek tool only processes one cell at a time. To scale this across multiple rows, you can write a simple VBA macro. This script will loop over a defined range (e.g., column N for gross margins) and adjust a corresponding changing cell (e.g., column L for selling prices) to meet your target value.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the VBA editor, go to the menu bar and click Insert > Module. This will create a blank space for your code.
Copy and paste the following code into the module window: Sub RunGoalSeek() Dim cell As Range For Each cell In Range("N2:N10") cell.GoalSeek Goal:=0.1, ChangingCell:=cell.Offset(0, -2) Next cell End Sub
Close the VBA editor, go to the Developer tab, click on Macros, select 'RunGoalSeek', and click Run. The script will automatically calculate the values for rows 2 through 10.

Automate Goal Seek in WPS Spreadsheet
WPS Spreadsheet fully supports VBA macros and advanced what-if analysis tools like Goal Seek, allowing you to seamlessly execute automation scripts just like in Microsoft Excel.
- 1. Open Your Dataset: Launch WPS Spreadsheet and open the document containing the rows you wish to analyze.
- 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click on 'VBA Editor', or simply press Alt + F11.
- 3. Run the Goal Seek Macro: Insert a new module, paste your Goal Seek loop script, and press F5 to run it. Your spreadsheet will instantly update the changing cells for all targeted rows.

Frequently Asked Questions
Why is my VBA Goal Seek macro returning a runtime error?
This usually happens if the 'ChangingCell' contains a formula instead of a hardcoded value, or if the 'Target Cell' does not contain a formula mathematically dependent on the changing cell. Ensure your target cell calculates its value based on the changing cell.
Can I use dynamic ranges instead of hardcoding row numbers?
Yes, you can make the range dynamic to handle any number of rows. Replace 'Range("N2:N10")' with 'Range("N2:N" & Cells(Rows.Count, "N").End(xlUp).Row)' to automatically loop through all populated rows in column N.
Can I reference the Goal value from another cell instead of hardcoding 0.1?
Absolutely. If your target goals vary row by row and are stored in an adjacent column, you can modify the code to reference that cell. For example: 'cell.GoalSeek Goal:=cell.Offset(0, 1).Value, ChangingCell:=cell.Offset(0, -2)'.
Does WPS Spreadsheet support VBA macros?
Yes, WPS Office Pro and certain customized versions offer full VBA support. You can write, edit, and run VBA scripts to automate tasks like Goal Seek directly within the application.




