+ Reply to Thread
Results 1 to 1 of 1

Multiple Goal Seek VBA Code

Hybrid View

excelwannabeee Multiple Goal Seek VBA Code 09-09-2012, 01:30 PM
  1. #1
    Registered User
    Join Date
    09-09-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    1

    Thumbs up Multiple Goal Seek VBA Code

    I have 200 rows that have different "Set cells","Desired Value" and "Changing Cell". For example, I would like to use Goal Seek that seeks each row with the changing variables. I pulled this code from a webiste and works to an extent. I pick the proper range for the variable, however only goal seeks the first row. I want to modify the code to goal seek the selected range and the CHANGING CELL must be >0. Thank you for help.


    Sub Multi_Goal_Seek()
        Dim TargetVal As Range, DesiredVal As Range, ChangeVal As Range, CVcheck As Range
        Dim CheckLen As Long, i As Long
    
    restart:
        With Application
            Set TargetVal = .InputBox(Title:="Select a range in a single row or column", _
                                      prompt:="Select your range which contains the ""Set Cell"" range", Default:=Range("L50:L54").Address, Type:=8)
            'no default option
            'prompt:="Select your range which contains the ""Set Cell"" range",, Type:=8)
            Set DesiredVal = .InputBox(Title:="Select a range in a single row or column", _
                                       prompt:="Select the range which the ""Set Cells"" will be changed to", Default:=Range("B50:B54").Address, Type:=8)
            'no default option
            'prompt:="Select the range which the ""Set Cells"" will be changed to",, Type:=8)
            Set ChangeVal = .InputBox(Title:="Select a range in a single row or column", _
                                      prompt:="Select the range of cells that will be changed", Default:=Range("M50:M54").Address, Type:=8)
            'no default option
            'prompt:="Select the range of cells that will be changed",, Type:=8)
        End With
    
        'Ensure that the changing cell range contains only values, no formulas allowed
        Set CVcheck = Intersect(ChangeVal, Union(Sheets(ChangeVal.Parent.Name).Cells.SpecialCells(xlBlanks), Sheets(ChangeVal.Parent.Name).Cells.SpecialCells(xlConstants)))
        If CVcheck Is Nothing Then
            MsgBox "Changing value range contains no blank cells or values" & vbNewLine & _
                   "Goal seek only works if the cells to be changed are values, please ensure that this is the case", vbCritical
            Application.GoTo reference:=DesiredVal
            Exit Sub
        Else
    
            If CVcheck.Cells.Count <> DesiredVal.Cells.Count Then
                MsgBox "Changing value range contains formulas" & vbNewLine & _
                       "Goal seek only works if the cells to be changed are values, please ensure that this is the case", vbCritical
                Application.GoTo reference:=DesiredVal
                Exit Sub
            End If
        End If
    
        'Ensure that the amount of cells is consistent
        If TargetVal.Cells.Count <> DesiredVal.Cells.Count Or TargetVal.Cells.Count <> ChangeVal.Cells.Count Then
            CheckLen = MsgBox("Ranges were different lengths, please press yes to re-enter", vbYesNo + vbCritical)
            If CheckLen = vbYes Then
                'If ranges are different sizes and user wants to redo then restart code
                GoTo restart
            Else
                Exit Sub
            End If
        End If
    
        ' Loop through the goalseek method
        For i = 1 To TargetVal.Columns.Count
            TargetVal.Cells(i).GoalSeek Goal:=DesiredVal.Cells(i).Value, ChangingCell:=ChangeVal.Cells(i)
        Next i
    End Sub

    Moderator Edit:

    Welcome to the forum.

    Please notice that code tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.
    Last edited by Cutter; 09-09-2012 at 03:55 PM. Reason: Added code tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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