VBA for looping GoalSeek based on If Then

VBAtrainingwheels

New Member
Joined
May 10, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi

Have been learning VBA for ~24 hrs, so apologies what may be a straightforward question, but I am trying to write a code for the following:
  1. I have the following rows of data in my sheet:
  • Row 21 in my sheet is a row of binary flag that either equals 1 or 0
  • Row 35 is a row of input values
  • Row 39 is a row of formulas calculating a percentage between 0% and 100%
  1. I would like to code the following logic:
  • If F21 <> 0, then GoalSeek M39 to equal 0.5 by changing cell F35
  • If F21 = 0, then move one column to the right (for each cell mentioned above)
  • Repeat until end the end of the range of binary flags in Row 21
My current code is below (I think I am potentially missing an offset or two on F35 and M39) - would be very grateful for any pointers.


VBA Code:
Sub DebtSize ()

‘Defined vairables

SevenYearPaydown = 0.5
FirstPaydownTest = Range(“M39”)
LastPaydownTest = Cells(39, Columns.Count).End(xlToLeft).Column

OpeningDebt = Range(“F35”)
FinalPeriodDebt = Cells(35, Columns.Count).End(xlToLeft).Column

FirstFlag = Range(“F21”)
LastFlag = Cells(21, Columns.Count).End(xlToLeft).Column

‘Debt sizing code

For x = FirstFlag To LastFlag

If FirstFlag <> 0 Then
FirstPaydownTest.GoalSeek Goal:=SevenYearPaydown, ChangingCell:=OpeningDebt
End If

Next x

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You want the changing cell in F35, G35, H35 and so on.
The formulas are in M39, N39, O39 and so on.
IF the above is correct, then try this:

VBA Code:
Sub DebtSize()
  Dim j As Long
  For j = Columns("F").Column To Cells(21, Columns.Count).End(xlToLeft).Column
    If Cells(21, j).Value = 1 Then Cells(39, j + 7).GoalSeek Goal:=0.5, ChangingCell:=Cells(35, j)
  Next
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
A follow up question - what would be the cleanest way to add a further constraint to the above code such that the outcome of the goal seek does not result in a calculation occurring in a different row exceeding a pre-determined cap (call the cap variable MaxLeverage, and the relevant calculation is in row 40)?

I guess you could add a further IF, but not sure if there is a cleaner way to introduce this constraint than a sequential goal seek:

VBA Code:

Sub DebtSize()
Dim j As Long
For j = Columns("F").Column To Cells(21, Columns.Count).End(xlToLeft).Column
If Cells(21, j).Value = 1 Then Cells(39, j + 7).GoalSeek Goal:=0.5, ChangingCell:=Cells(35, j)
If Cells(40, j).Value > MaxLeverage Then Cells(40, j).GoalSeek Goal:=MaxLeverage, ChangingCell:=Cells(35, j)
Next
End Sub

VBA Code:
 
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,600
Members
449,460
Latest member
jgharbawi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top