Buttons/Macros won't work on a copied sheet

christianrwevans

New Member
Joined
Jul 6, 2016
Messages
9
Probably an easy one here:
I have recorded simple macros to copy rows from other sheets into a sheet called "Scenario Builder 1". E.g.,:

Sub Thermal_Drying()
Sheets("Thermal Drying").Select
Rows("1:39").Select
Selection.Copy
Sheets("Scenario Builder 1").Select
ActiveCell.Insert Shift:=xlDown
End Sub

However when I make a copy of the sheet (e.g., "Scenario Builder 2"), the macro won't work as the reference is to "Scenario Builder 1". My novice intuition is that I need to define the sheet that hosts the button so that I can return to it to paste the rows, but my searching/skill level has not resulted in success yet.
Figured some of you could probably solve this sleep-walking so: what code should I use to 1) define the button host sheet in a way that returns the focus there to paste, or 2) to accomplish this task using a different approach?

Please and thank-you!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Maybe this way

VBA Code:
Sub Thermal_Drying()
ans = Application.InputBox("What number Scenario Builder do you want to use......1 Or 2 ?")
Sheets("Thermal Drying").Rows("1:39").Copy
Sheets("Scenario Builder " & ans).Range("A1").Insert
End Sub
 
Upvote 0
Thanks Michael M! Looks like that would do the trick - but do you think there is a way to skip the input component and have the macro automatically recognize where it is being launched from?
 
Upvote 0
Ok, so if your are launching from "Scenario Builder 1...or 2...or 3......." maybe use
VBA Code:
Sub Thermal_Drying()
Sheets("Thermal Drying").Rows("1:39").Copy
Activesheet.Range("A1").Insert
End Sub
 
Upvote 0
Yes that's the sort of thing I think I'm looking for. Poked around a bit, but can't figure out how to use the active cell as an insert location ("ActiveCell.Insert Shift:=xlDown"). What do you suggest?

I get an error with
VBA Code:
Sheets("Thermal Drying").Rows("1:39").Copy
ActiveCell.Insert Shift:=xlDown         'doesn't like this line

Even better would be the ability to search for a cell containing the text "Insert Here" and insert starting at that row. Thanks!
 
Upvote 0
VBA Code:
Sub Thermal_Drying()
Sheets("Thermal Drying").Rows("1:39").Copy
ActiveCell.Insert
End Sub
 
Upvote 0
The above will error if the activecell is not in Column "A" !!
This will use the row of whatever cell is the activecell
VBA Code:
Sub Thermal_Drying()
Sheets("Thermal Drying").Rows("1:39").Copy
Range("A" & ActiveCell.Row).Insert
End Sub
 
Upvote 0
And this will search for "INSERT HERE"......in Upper Case and insert the rows at the row above the found text
VBA Code:
Sub Thermal_Drying()
Dim fr As Long
fr = Cells.Find("INSERT HERE", , xlValues, , xlRows, xlPrevious).Row
Sheets("Thermal Drying").Rows("1:39").Copy
Range("A" & fr).Insert
End Sub
 
Upvote 0
Solution
And this will search for "INSERT HERE"......in Upper Case and insert the rows at the row above the found text
VBA Code:
Sub Thermal_Drying()
Dim fr As Long
fr = Cells.Find("INSERT HERE", , xlValues, , xlRows, xlPrevious).Row
Sheets("Thermal Drying").Rows("1:39").Copy
Range("A" & fr).Insert
End Sub
Bullseye! Super grateful to you Michael M, this works like a charm. Thanks so much for your generosity, patience, and expertise!
 
Upvote 0
Glad to help and thanks for the feedback... :cool: (y)(y)
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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