Forum

VBA to copy a new s...
 
Notifications
Clear all

VBA to copy a new sheet based and then access new sheet with a variable

9 Posts
3 Users
0 Reactions
70 Views
(@kwesmc1)
Posts: 31
Trusted Member
Topic starter
 

I am trying to copy a new sheet based on the active sheet and rename it using an input box. I then want to copy a sheet named "January" and copy and paste that data into the worksheet I just created, but it needs to be a variable named worksheet as I add a new sheet each month.
In the code below, "March" in this case will be named to April for the following month.
I hope this explanation makes sense. I can provide the workbook if needed.

Can someone please help. Here is what I have so far.
Thanks in advance,
Ken Mc

Sub InsertDuplicateSheet() 
    ActiveSheet.Copy After:=ActiveSheet 
    ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet") 
    Sheets("January").Select 
    Range("A1").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy 
    Sheets("March").Select 
    Range("A1").Select 
    ActiveSheet.Paste 
    Range("B4").Select 
     
End Sub 

 
Posted : 06/02/2018 11:53 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Kenneth

Try this. I added the code in red.

Sub InsertDuplicateSheet()
ActiveSheet.Copy After:=ActiveSheet
ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet")
'Save the newly created sheet name to a variable
NewShtName = ActiveSheet.Name
Sheets("January").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Copy to the newly created sheet
Sheets(NewShtName).Select
Range("A1").Select
ActiveSheet.Paste
Range("B4").Select
End Sub

Hope this helps.

Sunny

 
Posted : 07/02/2018 8:36 pm
(@david_ng)
Posts: 310
Reputable Member
 

Cannot run the VB, prompt syntax error

Pls try store to a worksheet and run to see if problem can be fixed

 
Posted : 08/02/2018 1:49 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi David

You need 1 default sheet and another sheet named January.

The modified code is based on my understanding on what the OP wanted and also what the original code is doing.

Without the actual file with some sample data, this is all that I can do (making a guess and hope for the best Kiss)

Sunny

 
Posted : 08/02/2018 2:07 am
(@kwesmc1)
Posts: 31
Trusted Member
Topic starter
 

Thanks David and Sunny!!

I really appreciate your help. I added your variable as a string and changed:

 NewShtName = ActiveSheet.Name

and everything worked perfectly.

Thanks again,

Ken Mc

 
Posted : 08/02/2018 11:03 am
(@sunnykow)
Posts: 1417
Noble Member
 

Thanks for the feedback Kenneth.

Glad we are able to help.

Sunny

 
Posted : 08/02/2018 7:25 pm
(@david_ng)
Posts: 310
Reputable Member
 

Ken,

If you don't mind can you upload the Worksheet with the VB  for our reference , of course all sensitive or cret data to be 

covered-up or twisted as purely for testing application objective.

 
Posted : 08/02/2018 10:27 pm
(@kwesmc1)
Posts: 31
Trusted Member
Topic starter
 

Of course not David. It's just chart I made to track the problem I was having with my feet and hands. I'm happy to say since the doc put me on a steroid, I've made great progress since late November. 🙂Laugh

 
Posted : 09/02/2018 1:50 pm
(@david_ng)
Posts: 310
Reputable Member
 

Thanks millions Ken!

Honestly we learn a lot from this forum!

 
Posted : 09/02/2018 7:47 pm
Share: