Solved

Copy current sheet using VBA

Posted on 2015-01-20
3
66 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 51

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

738 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