Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy current sheet using VBA

Posted on 2015-01-20
3
Medium Priority
?
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

722 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