Amateur VBA question - need help (urgent!)

So I wrote the macros for my model to run for various scenarios and the model runs fine. But now I need to add an if statement where if the toggle cell I gave the name "GoalSeeker" has "Yes" selected, I want the model to goalseek the growth rate instead of using the assigned growth rate (cell I labeled as "Growth_rate_new").

Here's the problem... the if stmt. in the macro isn't picking up the differentiation and not doing the goalseek. Can anyone tell me why it's wrong? I'm pretty new at this.

Here's the code I'm wrote:

If GoalSeeker = "Yes" Then

 Sheets("ABC").Select
    Range("C21").Select
        ActiveCell.GoalSeek Goal:=0, ChangingCell:=Range("H13")

    Else

 Sheets("<span><a href="/finance-dictionary/what-is-internal-rate-of-return-IRR"><abbr>IRR</abbr></a></span>").Select
     Range("Growth_rate_new").Select
     Selection.Copy
     Sheets("ABC").Select
     Range("H13").Select
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
     End If

C21 is the cell that I am goalseeking to 0
H13 will be the growth rate used

Thanks

 

If Range("GoalSeeker").Value = "Yes" Then

Worksheets("ABC").Range("C21").GoalSeek Goal:=0, ChangingCell:=Worksheets("ABC").Range("H13")

Else

Worksheets("ABC").Range("H13").Value = Sheets("IRR").Range("Growth_rate_new").Value

End If

 

Thanks dacarez much appreciated!

Yeah I know, the code I'm writing is pretty inefficient... I kind of just been learning how to write it after recording the action...

Is there a way for the macros to recognize change in worksheet names? Seems like the references are absolute... so if I change worksheet name to XYZ from ABC, the code won't pick it up right?

 

Nevermind, I just reread what you are trying to do.

Jack: They’re all former investment bankers who were laid off from that economic crisis that Nancy Pelosi caused. They have zero real world skills, but God they work hard. -30 Rock
 
Best Response

Always nice to see bankers getting VBA-enabled, definitely gives you more options on the fringe.

Right, those names are hardcoded. You can do one of two things 1 - declare constant variables at the top of your module with sheet/range names that you can just change once and it will flow through 2 - put something in the sheet that identifies the sheet and then write a function that returns that sheet.

e.g. 1 - Const aSheet as String = "ABC" Const iSheet as String = "IRR"

and then in your code: Worksheets(aSheet).Range("A1")....

2 - Say that Cell A1 has identifier text of ABC for sheet currently called ABC and XYZ for another sheet Function FindSheet(strIdent as String) As Worksheet Dim cSheet as Worksheet For Each cSheet in ThisWorkbook.Worksheets If cSheet.Range("A1").Value = strIdent Then Set FindSheet = cSheet Exit For End If End Function So that in your code in other places, instead of referring to Worksheets("ABC"), you could do:

Dim ABCSheet as Worksheet Set ABCSheet = FindSheet("ABC")

(you use Set for Object assignment in VBA, and you want to save that reference because the For loop is a bit time-intensive)

 

You could just do an absolute reference to the sheet number. For example, even though you may rename Sheet1 to ABC, you can still refer to that sheet as Sheet1.Select, or whatever you want to do.

Jack: They’re all former investment bankers who were laid off from that economic crisis that Nancy Pelosi caused. They have zero real world skills, but God they work hard. -30 Rock
 
Revsly:
You could just do an absolute reference to the sheet number. For example, even though you may rename Sheet1 to ABC, you can still refer to that sheet as Sheet1.Select, or whatever you want to do.
You could - it works - but it makes your code very hard to read/follow/debug.
 

Well, of course you can make it clean by introducing a const variable at the begining like ABCSheet = Sheet1.Name to make it more readable, along with other things. I just figured I'd point out a simple thing that people often overlook.

Jack: They’re all former investment bankers who were laid off from that economic crisis that Nancy Pelosi caused. They have zero real world skills, but God they work hard. -30 Rock
 

Repudiandae ipsam consequatur animi unde. Eum et deleniti dolores ut est. Enim minus voluptatem perspiciatis dolorem rerum asperiores.

Enim nisi repellendus nobis alias qui officia exercitationem. Quas vel repellat recusandae ex dolorum. Quasi suscipit et ea molestiae dolorem.

Omnis ut facilis optio tempore tempore quis maxime. Eos quasi aut aut fuga aut. Consequuntur nisi repellendus enim maiores dolor modi aut.

Illum eum aut et quia. Illum doloribus dolorem sunt est aut illo odio. Sed maiores aut quo ipsum et et. Distinctio iusto et fugiat aut quo omnis. Est praesentium dolorem earum ullam veritatis eos.

Career Advancement Opportunities

April 2024 Investment Banking

  • Jefferies & Company 02 99.4%
  • Goldman Sachs 19 98.8%
  • Harris Williams & Co. New 98.3%
  • Lazard Freres 02 97.7%
  • JPMorgan Chase 03 97.1%

Overall Employee Satisfaction

April 2024 Investment Banking

  • Harris Williams & Co. 18 99.4%
  • JPMorgan Chase 10 98.8%
  • Lazard Freres 05 98.3%
  • Morgan Stanley 07 97.7%
  • William Blair 03 97.1%

Professional Growth Opportunities

April 2024 Investment Banking

  • Lazard Freres 01 99.4%
  • Jefferies & Company 02 98.8%
  • Goldman Sachs 17 98.3%
  • Moelis & Company 07 97.7%
  • JPMorgan Chase 05 97.1%

Total Avg Compensation

April 2024 Investment Banking

  • Director/MD (5) $648
  • Vice President (19) $385
  • Associates (87) $260
  • 3rd+ Year Analyst (14) $181
  • Intern/Summer Associate (33) $170
  • 2nd Year Analyst (66) $168
  • 1st Year Analyst (205) $159
  • Intern/Summer Analyst (146) $101
notes
16 IB Interviews Notes

“... there’s no excuse to not take advantage of the resources out there available to you. Best value for your $ are the...”

Leaderboard

success
From 10 rejections to 1 dream investment banking internship

“... I believe it was the single biggest reason why I ended up with an offer...”