Goal Seek with Excel Macro

john1212

New Member
Joined
Jul 21, 2022
Messages
1
I am using a record of an Excel Macro for a Goal Seek function on Excel. The problem is that when I try to add a new row in my sheet (both above the cells running the Goal Seek or even in the middle of them), the Macro that i recorded cannot be used anymore and I need to record it again (the error that i receive if i try to run it is:"1004, reference is not valid"). How could I fix this so that I don't need to record everytime the same Macro when I add/delete a row? Please help me. I attach below the pics and the VBA code

Code:
Sub Macro17()
'
' Macro17 Macro
'

'
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveCell.GoalSeek Goal:=0, ChangingCell:=ActiveCell.Offset(-9, 0).Range( _
        "A1")
End Sub
 

Attachments

  • 2fhQ8.png
    2fhQ8.png
    6.1 KB · Views: 13
  • 7XPib.png
    7XPib.png
    18.2 KB · Views: 12

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I am using a record of an Excel Macro for a Goal Seek function on Excel. The problem is that when I try to add a new row in my sheet (both above the cells running the Goal Seek or even in the middle of them), the Macro that i recorded cannot be used anymore and I need to record it again (the error that i receive if i try to run it is:"1004, reference is not valid"). How could I fix this so that I don't need to record everytime the same Macro when I add/delete a row? Please help me. I attach below the pics and the VBA code

Code:
Sub Macro17()
'
' Macro17 Macro
'

'
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveCell.GoalSeek Goal:=0, ChangingCell:=ActiveCell.Offset(-9, 0).Range( _
        "A1")
End Sub
Hi. Best course of action in my opinion would be to save the Goal Seek cells as a Named Range. There are several ways to do this, but the fastest would be to type a name into the Name Box to the left of the formula bar with the changing cell selected. Also do the same with a different name for the result cell.

Next you can make modifications to the code as follows:

Remove both Application.CutCopyMode = False (not needed). I don't know what the name of your sheet is, so replace the name I put in the next line with it's name. Also change the names I use inside the Ranges to whatever you used for the result & changing cells.

VBA Code:
Sub Macro 17()
Worksheets("Sheet X").Range("GoalSeekResultCell").GoalSeek Goal:=0, ChangingCell:=Worksheets("Sheet X").Range("ChangeCell")
End Sub

That should work, but I haven't tested it since I'm on mobile.
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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