Macro copy to new sheet and copy that sheet after format cells

ether

New Member
Joined
Sep 4, 2021
Messages
4
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I created a Marco to copy a worksheet named "Data" to a new worksheet called "Sept"

Then unmerged all the cells in "Sept", and copied it to a new worksheet named "Oct"

The problem I'm running into is that it only works on new workbooks.

If I use it on a workbook with data already in it, when I get here: Worksheets("Sept").Copy After:=Worksheets("Sept")

It says the 'Click_to_Select_Amount' already exist.

I don't have another worksheet named Oct, so I'm not sure what's going on.

Any ideas?

VBA Code:
Sub Copy()
'   
    Set ws = ActiveSheet
    ActiveSheet.Name = "Data"
    ws.Copy After:=Worksheets(Sheets.Count)
    ActiveSheet.Name = "Sept"
    ActiveSheet.Cells.UnMerge
    Worksheets("Sept").Copy After:=Worksheets("Sept")
'
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I don't have another worksheet named Oct, so I'm not sure what's going on.
I am a bit confused, nowhere in the code do you mention Oct.

If you want to the newly created sheet to be called Oct then you need to add the line below to your code (same as you have already done for Sept)
VBA Code:
    ActiveSheet.Name = "Oct"

As your code stands you won't be able to run it a 2nd time on the same workbook since at the sheet Sept would already exist on the 2nd run and if you add the above line so will Oct.
 
Upvote 0
I am a bit confused, nowhere in the code do you mention Oct.

If you want to the newly created sheet to be called Oct then you need to add the line below to your code (same as you have already done for Sept)
VBA Code:
    ActiveSheet.Name = "Oct"

As your code stands you won't be able to run it a 2nd time on the same workbook since at the sheet Sept would already exist on the 2nd run and if you add the above line so will Oct.

How would I write the code, so I don't have this problem?

Thanks for your help!
 
Upvote 0
You need to provide more details on what you are trying to do, especially when you run it repeatedly.
What do you expect to happen the 2nd time you run it and then the 3rd time etc.

If you are adding a month each time are you starting with the sheet Data and have to do the unmerge each time ?
Do you want an input box to ask the user for the new month name ?
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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