Results 1 to 6 of 6

Thread: Create New Sheet-Copy, Paste

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    21

    Create New Sheet-Copy, Paste

    I would like to do a workbook close event where a new workbook is created, and the active sheet gets copied and pasted to it. I also need it to paste the information in the cells only, not the functions.

    Any ideas?
    Last edited by Leah; Apr 16th, 2008 at 10:52 AM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create New Sheet-Copy, Paste

    Quote Originally Posted by Leah
    I would like to do a workbook close event where a new workbook is created, and the active sheet gets copied and pasted to it. I also need it to paste the information in the cells only, not the functions.

    Any ideas?
    Please note that you may have to make relevant changes

    Paste this in the before close event of the workbook from where you want to copy the data.

    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Call CopyandSaveNew
    End Sub
    and paste this in a module

    Code:
    Sub CopyandSaveNew()
        'DECLARE OBJECTS
        Dim ApExcel As Object, WbExcel As Object, str1 As String, str2 As String
        
        'Predecide the name of the New workbook
        str1 = "NewBook.xls"
        str2 = ActiveWorkbook.Name
        
        On Error Resume Next
        
        Set ApExcel = GetObject(, "Excel.application") 'SEE IF ANY EXISTING EXCEL IS OPEN
            
        If ApExcel Is Nothing Then
            'IF NO INSTANCE OF EXCEL IS FOUND THEN CREATE ONE
            Set ApExcel = CreateObject("Excel.application")  'CREATE A NEW EXCEL APPLICATION
        End If
    
        ApExcel.Visible = True ' So you can see Excel
        
        'Add new workbook
        Set WbExcel = ApExcel.Workbooks.Add
                
        'Save the newly created workbook at the required path
        WbExcel.SaveAs "c:\temp\" & str1
        
        'Select the workbook from "where" you want to copy the code
        Windows(str2).Activate
        Sheets(1).Cells.Select
        Selection.Copy
        
        'Select the workbook where you want to copy the code
        Windows(str1).Activate
        Cells.Select
        ActiveSheet.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        
        'SAVE AND CLOSE WORKBOOK
        WbExcel.Save
        WbExcel.Close
        Set WbExcel = Nothing
            
        'QUIT EXCEL
        ApExcel.Quit
        Set ApExcel = Nothing
    
    End Sub
    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    21

    Re: Create New Sheet-Copy, Paste

    Thank you. I forgot to ask. Will this also copy and paste the pictures I have on the old sheet?

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create New Sheet-Copy, Paste

    yes it will if you replace this line in the above code

    Code:
    ActiveSheet.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks 
            :=False, Transpose:=False
    with

    Code:
    ActiveSheet.Paste 'but this will not paste values...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    21

    Re: Create New Sheet-Copy, Paste

    Okay, I need my user to do the saving. Actually, this little snippet here works perfectly, except that it copies and pastes the functions in the cells, and I can't have that, as the filepaths won't match.


    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Call CopyNew
    
    End Sub
    Sub CopyNew()
        
     With ActiveSheet
          .Select
          .Copy
          .Protect "fox4704"
       End With
        
    End Sub

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create New Sheet-Copy, Paste

    Sub CopyNew()
    With ActiveSheet
    .Select
    .Copy
    .Protect "fox4704"
    End With
    End Sub
    Is this code enough to do what you wanted???
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width