Report

Copy paste Rename based on cell value [Solved]

Ask a question Josephnb 2Posts Tuesday November 15, 2016Registration date November 16, 2016 Last seen - Last answered on Nov 21, 2016 08:28AM
Hello

I have a worksheet named template. I need a VBA code which would copy "template" automatically into a new worksheet and rename the new worksheet based on the value from two cells in "template".

Thanks
See more 
Helpful
+0
moins plus
The easiest way to achieve that is to start recording a new macro, then manually do the operations (creating a new worksheet, copying template to the new worksheed, renaming the new worksheet.
Once you have that macro, you will easily (perhaps with some assisitance from this forum) adapt it according to your needs.
Add comment
Helpful
+0
moins plus
Sorry, should have added the code

Sub Copyrenameworksheet()

Dim ws As Worksheet
Set wh = Worksheets(ActiveSheet.Name)
ActiveSheet.copy After:=Worksheets(Sheets.Count)
If wh.Range("i9").Value <> "" Then
ActiveSheet.Name = wh.Range("i9").Value

End If
'go back to original sheet and delete cell content. Original sheet being used as the template

Wh.Activate
Range("D11:F11, H11:J11").Select
selection.ClearContents


'Would like to go to the new sheet here

Wh.range("I9").Activate

End Sub

-------------------------
Problem:

1) i would like the code to run automatically when cell D11 & H11 are not blank. The code works but I need to click a button
2) I would like the"new" worksheet to open and be automatically renamed based on cell I9. The code renames the sheet but does not open the "new" worksheet.
3) I would like cells D11 & H11 in the "template" to clear once the "new worksheet is created and opened.
yg_be 842Posts lundi 9 juin 2008Registration date December 8, 2016 Last seen - Nov 16, 2016 10:12AM
For items 2, I assume you want the "new" worksheet to be created automatically, is that right?
For items 2 & 3 : start recording a new macro, then manually do the operations you want to automate, then stop recording. Then have a look into the code of the recorded macro, and enhance your macro accordingly.
Reply
Joe- Nov 17, 2016 01:56PM
1) you are correct with your assumption for item 2
2) I've done what you have suggested with 3 different macros but I still can not get the new page, which is renamed based on my code above to open to the new page. When I run the Macro, it copies, paste and renames just will not open to it.
Reply
yg_be 842Posts lundi 9 juin 2008Registration date December 8, 2016 Last seen - Nov 17, 2016 02:15PM
Do you expect that
Wh.range("I9").Activate 
will activate your new sheet? It actually activates the cell I9 in the sheet corresponding to the variable wh, hence in your template sheet.
There are various ways to correct that. Here is my suggestion :
Replace your code :
'go back to original sheet and delete cell content. Original sheet being used as the template 
Wh.Activate 
Range("D11:F11, H11:J11").Select 
selection.ClearContents 
'Would like to go to the new sheet here 
Wh.range("I9").Activate

by
' delete cell content in original sheet being used as the template 
Wh.Range("D11:F11, H11:J11").ClearContents 
'nothing to do, we are already in the new sheet here 

Suggestion : avoid using select and activate in VBA.
Reply
yg_be 842Posts lundi 9 juin 2008Registration date December 8, 2016 Last seen - Nov 21, 2016 02:25AM
Does that work now?
Reply
Josephnb- Nov 21, 2016 08:28AM
Great thank you. It's the "wh" that I was missing.

Thanks for the help!
Reply
Add comment

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!