Solved

Copy current sheet using VBA

Posted on 2015-01-20
3
63 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
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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

932 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