+ Reply to Thread
Results 1 to 3 of 3

Help with Goal Seek Macro

  1. #1
    Registered User
    Join Date
    07-28-2022
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Lightbulb Help with Goal Seek Macro

    I have 76 sheets where I need to perform a goal seek, so I'm attempting to get this to work in VBA... But I'm getting runtime errors. I have someone's account value in I3. Their final payment date is in I4. The spreadsheet looks up the final payment cell and puts it into I5, and I6 adjusts the cell value to show what cell we would want to "set to 0" to avoid having to constantly scroll when I was doing this manually.

    I need to:
    Set Cell: (value of I6)'
    To Value: 0
    By changing Cell: F4

    Here is my most recent attempt that resulted in a runtime error. I've also attached my spreadsheet as an example. Any help would be greatly appreciated!

    Goal Seek Macro.xlsm

    Sub PerformGoalSeekOnSheets()
    Dim ws As Worksheet
    Dim targetCell As Range
    Dim changingCell As Range
    Dim targetCellAddress As String ' Store the address of the target cell
    Dim colLetter As String ' Store the column part of the cell reference
    Dim rowNumber As Long ' Store the row part of the cell reference

    ' Define the changing cell (F4) only once
    Set changingCell = ThisWorkbook.Worksheets("TemplateSheet").Range("F4")

    ' Loop through each worksheet starting from the third sheet
    For Each ws In ThisWorkbook.Worksheets
    If ws.Index >= 3 Then
    ' Evaluate the formula in cell I6 to get the actual cell reference
    targetCellAddress = ws.Range("I6").Value

    ' Print the value of cell I6 to the Immediate Window
    Debug.Print "Value of cell I6 on worksheet '" & ws.Name & "': " & targetCellAddress

    ' Check if the target cell address is valid
    If Not IsValidCellReference(targetCellAddress, colLetter, rowNumber) Then
    MsgBox "Invalid cell reference in cell I6 on worksheet '" & ws.Name & "'. Skipping goal seek operation.", vbExclamation
    Else
    ' Set the target cell using the column and row parts
    Set targetCell = ws.Cells(rowNumber, colLetter)

    ' Perform goal seek
    Application.GoalSeek Goal:=0, ChangingCell:=changingCell, _
    ChangingCellFormula:=changingCell.Formula
    End If
    End If
    Next ws
    End Sub

    Function IsValidCellReference(cellRef As String, ByRef colLetter As String, ByRef rowNumber As Long) As Boolean
    Dim i As Integer
    Dim colPart As String
    Dim rowPart As String
    Dim hasFoundNumeric As Boolean

    ' Initialize variables
    colLetter = ""
    rowNumber = 0
    hasFoundNumeric = False

    ' Check if the cell reference is in the format of "COLROW" (e.g., "D5453")
    If Len(cellRef) < 2 Then Exit Function

    ' Extract the column part (letters) and row part (numbers)
    For i = 1 To Len(cellRef)
    If Not hasFoundNumeric Then
    If IsNumeric(Mid(cellRef, i, 1)) Then
    colPart = Left(cellRef, i - 1)
    rowPart = Mid(cellRef, i)
    hasFoundNumeric = True
    End If
    End If
    Next i

    ' Check if the column part is valid (A-Z or a-z)
    If colPart Like "[A-Za-z]*" Then
    colLetter = colPart
    rowNumber = Val(rowPart)
    IsValidCellReference = True
    End If
    End Function

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,628

    Re: Help with Goal Seek Macro

    The issue is that your changingCell contains value not formula so changingCell.Formula returns constant value.

    So to do this properly: First clearly state what is your goal.

    EDIT - you can almost surely skip to "But when i went down here writing" below.


    First option:

    I'd asume that it is several steps. First finding in which row we have last positive value in column D
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    it is 1465
    then find the date in next row (1466) - in older versions of Excel I'd use INDEX for that, but as you have version 365 I'd use for both steps in I4:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Then your I5 and I6 give right addresses.

    And one could use Goalseek to change value in C1466 so the goal will be D1466 C1466 shall be changed into value and used as ChangingCell in goalseek.


    But when i went down here writing, I'm almost for sure into the second option. Your goal is to have 0 in column D at a date given (not found by formula) in I4

    So you want to do this by changing F4 value (flat monthly payment)

    If so, then just a small correction to your code would do:

    Please Login or Register  to view this content.
    Probably code can be simplified quite a bit. But let's look if the above suggestion works.


    BTW - see how using CODE tags improves readability of the code

    PS. Note that you see just 2 decimal places, while calculations are done with more decimal places. So there will be some small discrepancies at the end if you compare result with the banking system.
    Last edited by Kaper; 03-15-2024 at 07:41 AM.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    07-28-2022
    Location
    USA
    MS-Off Ver
    365
    Posts
    13

    Re: Help with Goal Seek Macro

    I thought I had responded to this that day, but I think I just typed it and did not hit post. The answer was your second option! It fixed it and worked like a charm and I saved myself HOURS of work, so I appreciate your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Goal Seek Macro
    By markvdhouten in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-08-2021, 06:06 PM
  2. [SOLVED] Goal Seek Macro
    By Adamsc21 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2017, 05:22 PM
  3. Goal seek in a Macro
    By Wizards in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 04:04 AM
  4. Goal Seek Macro?
    By caj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2012, 06:19 AM
  5. Goal Seek Macro
    By stephenp12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2011, 06:06 PM
  6. Macro, goal seek
    By bilbonvidia in forum Excel General
    Replies: 1
    Last Post: 07-10-2007, 09:33 PM
  7. Macro help for goal seek
    By Dean in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2005, 01:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1