+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Talking MAcro to Copy Worksheet and Worksheet Events

    Hi,
    I have a worksheet that utilizes a Worksheet Change Event. I created a macro to copy the sheet and that works fine except for the fact that it doesn't contain the Worksheet Change event. Any idea on how I can do this (if possible) in a macro?

    Thanks in advance.

    Max57
    Last edited by max57; 07-28-2009 at 09:32 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    395

    Re: MAcro to Copy Worksheet and Worksheet Events

    not clear.
    suggest you call the macro from within the worksheet change event
    worksheet change event occurs when target value changes.

  3. #3
    Valued Forum Contributor
    Join Date
    08-22-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    207

    Re: MAcro to Copy Worksheet and Worksheet Events

    i assume you might mean -
    Code:
    Sub CopyToNewWB()
    ' copies Sheet1 to a new workbook - sheet code included
        Activeworkbook.Sheets("Sheet1").Copy after:=Sheets(Sheets.Count)
    End Sub
    
    Sub CopyAsNewSht()
    ' creates copy of Sheet1 in sameworkbook - sheet code included
        Activeworkbook.Sheets("Sheet1").Copy
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: MAcro to Copy Worksheet and Worksheet Events

    Hi all,
    I had originally used a macro recorder macro and that's where my problem originated from. After reading the posts, I realized that I wasn't copying the Worksheet rather, only the worksheet cells. As a result, I wasn't getting the Worksheet Change event code copied into the new sheet.

    I found a very good snippet of code that added a little more functionality to what I wanted to do and adopted it to my needs.

    Code:
    Option Explicit
    
    Dim NewPageName As String
    
    Sub NewPage()
    Dim Sheets As Worksheet
    Set Sheets = Sheet4
    Sheets.Visible = True
    Sheets.Copy After:=Worksheets(Worksheets.Count)
    NewPageName = InputBox("What would you like to call your new Worksheet")
    ActiveWindow.ActiveSheet.Name = NewPageName
    Sheets.Visible = False
    
    End Sub
    Thank you for your suggestions which inevitably led me to this conclusion.

    Max57

  5. #5
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: MAcro to Copy Worksheet and Worksheet Events

    I wouldn't declare sheets as a variable, use sht or similar instead
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  6. #6
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: MAcro to Copy Worksheet and Worksheet Events

    Point well taken.
    Thanks for the heads up.

    Max57

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0