Solved

Copy current sheet using VBA

Posted on 2015-01-20
3
62 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 25

Expert Comment

by:ProfessorJimJam
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ms sql stored procedure 22 74
IF Statement 3 23
conditional formatting formula error 4 16
Checkbox Notification 3 22
A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now