简体   繁体   中英

Copy Entire Worksheet to Another Worksheet

I have an excel workbook that currently has a macro built into it that when pressing the macro's button, the current workbook is saved to a directory that the macro creates and the template is then wiped clean in order to make room for the next day's data.

Instead of this macro saving the entire workbook as a new file that then has to be tracked down to be viewed, I would like to edit the macro to do the following:

  1. Do not create or save a new file.
  2. When "New Day" macro is clicked, move what is in the template worksheet titled "Morning Report" to a new worksheet.
  3. The name of the new tab should be the date of the moved template, which is located at W1.

Here is the code:

Sub NewDay()

Dim fn, d, ln
Set fs3 = CreateObject("Scripting.FileSystemObject")
Set fs = CreateObject("Scripting.FileSystemObject")
If fs3.FolderExists("c:\Morning Reports\") = True Then GoTo ext Else GoTo mkdir:
mkdir:
  Set d = fs.CreateFolder("c:\Morning Reports\")
  With Application
    .UserName = "MD Totco"
    .StandardFont = "Arial"
    .StandardFontSize = "10"

    .DefaultFilePath = "c:\Morning Reports\"
    .EnableSound = False
    .RollZoom = False
  End With
ext:
  Sheets("Morning Report").Select
    ActiveSheet.Unprotect

  ChDir "c:\Morning Reports\"
    tempdate = ActiveSheet.Range("w1") + 1
    ActiveSheet.Range("w1") = tempdate
    tDate = Format(tempdate, "mmm d yyyy")
    WellName = ActiveSheet.Range("s2")

    fName = tDate + ", " + WellName
    ActiveWorkbook.SaveAs FileName:=fName

This would be the section within Sub NewDay that is creating the new file and saving, the rest of the code is wiping and refreshing the template. However, I do not want the workbook to be saved, I just want the "Morning Report" sheet of the workbook to be copied over to a new sheet.

Copying a worksheet is easy:

Public Sub copy()
 Sheets(1).copy after:=Sheets(1)
End Sub

The remainder should be easy for you to figure out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM