Solved

Copy current sheet using VBA

Posted on 2015-01-20
3
64 Views
Last Modified: 2015-01-20
Dear Experts:

could somebody help me write the following VBA code:

Copy the current worksheet, and name it 'current_month'.
If a worksheet with the name 'current_month' already exists, that sheet is to be deleted it, but in that case a prompt ...
... should inform the user: "Sheet 'current_month' already exists. Would you like to delete it?" If pressed yes, the sheet named 'current_month' gets deleted first and the new one is created. If pressed no, the macro exits and nothing happens

Help is very much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
3 Comments
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40559402
here you go

Sub copysh()
 
 Dim W As Worksheet
For Each W In ThisWorkbook.Worksheets
If W.Name = "Current_Month" Then
MsgBox "Sheet Current Month ALready Exists"
Exit Sub
Else
ActiveSheet.Copy ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = "Current_Month"
 End If
 Next W
End Sub
 

Open in new window

0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40559469
pls try
Sub macro()
On Error Resume Next
Set CurrWs = ThisWorkbook.Worksheets("Current_Month")
On Error GoTo 0
If Not IsEmpty(CurrWs) Then
    Res = MsgBox("Sheet Current_Month already exists. Do you want to delete it?", vbYesNo)
        If Res = vbYes Then
            Application.DisplayAlerts = False
            CurrWs.Delete
            Application.DisplayAlerts = True
        Else
            Exit Sub
        End If
End If
ActiveSheet.Copy after:=ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = "Current_Month"

End Sub

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 40559866
Dear JimJam, I am sorry to tell you that your code throws an 1004 Error message.

Hi Rgonzo, thank you very much for your swift and professional help. The code works as desired. Thank you very much.

Regards, Andreas
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question