+ Reply to Thread
Results 1 to 6 of 6

Thread: Copy worksheet to after last worksheet of another workbook

  1. #1
    Registered User
    Join Date
    05-05-2010
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Cool Copy worksheet to after last worksheet of another workbook

    Hello, I'm currently working through Excel 2007 Programming VBA for Dummies book, and I'm having trouble with what I want excel to do.
    Using the code below, I can get the activesheet name changed but it will only copy to after the FIRST sheet in another workbook.
    Am I missing something here? Are there any attributes I should check in the workbook i'm copying to/from?

    Sub Macro1()
    '
    '  Macro1 Macro
    '
    '  Keyboard Shortcut: Ctrl+Shift+D
    '
    '  Every macro should have this of course...
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    '  Start main code
        ActiveSheet.Name = Range("H13").Value
        ActiveSheet.Copy After:=Workbooks("APRIL 10.xls").Sheets(Sheets.Count)
    '  End main code
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Thanks for your help

    btw: This site looks awesome!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Copy worksheet to after last worksheet of another workbook

    Hello Giant_Cheeseman,

    Welcome to the Forum!

    To make your code easier to read and more flexible, you can assign object variables to the workbooks.
    Sub Macro1()
    '
    '  Macro1 Macro
    '
    '  Keyboard Shortcut: Ctrl+Shift+D
    '
    Dim DstWkb As Workbook
    
      Set DstWks = Workbooks("APRIL 10.xls")
    
    '  Every macro should have this of course...
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    '  Start main code
        ActiveSheet.Name = Range("H13").Value
        ActiveSheet.Copy After:=DstWkb.Sheets(DstWkb.Sheets.Count)
    '  End main code
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-05-2010
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy worksheet to after last worksheet of another workbook

    Quote Originally Posted by Leith Ross View Post
    Sub Macro1()
    '
    '  Macro1 Macro
    '
    '  Keyboard Shortcut: Ctrl+Shift+D
    '
    Dim DstWkb As Workbook
    
      Set DstWks = Workbooks("APRIL 10.xls")
    
    '  Every macro should have this of course...
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    '  Start main code
        ActiveSheet.Name = Range("H13").Value
        ActiveSheet.Copy After:=DstWkb.Sheets(DstWkb.Sheets.Count)
    '  End main code
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    This code doesn't work. When I run debug, it highlights the line I highlighted above.
    The original code I wrote would actually change the name of the sheet at least.
    Any help would be appreciated.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Copy worksheet to after last worksheet of another workbook

    Hello Giant_Cheeseman,

    That's my fault due to a typo. I corrected the typo in this code.
    Sub Macro1()
    '
    '  Macro1 Macro
    '
    '  Keyboard Shortcut: Ctrl+Shift+D
    '
    Dim DstWkb As Workbook
    
      Set DstWkb = Workbooks("APRIL 10.xls")
    
    '  Every macro should have this of course...
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    '  Start main code
        ActiveSheet.Name = Range("H13").Value
        ActiveSheet.Copy After:=DstWkb.Sheets(DstWkb.Sheets.Count)
    '  End main code
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    05-05-2010
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy worksheet to after last worksheet of another workbook

    Ah, yes that worked. However only after I realised my macro key stopped working. I have reset it in macro options, and the code worked.

    Thanks!

  6. #6
    Registered User
    Join Date
    05-05-2010
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy worksheet to after last worksheet of another workbook

    Just a quick question, could this code have an attachment where in the same process, it could save the workbook as the value defined (H13)?

    Or potentially save the sheet as a workbook with the sheet name (might be easier)?

+ Reply to Thread

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