+ Reply to Thread
Results 1 to 20 of 20

VBA copy and paste sheet from one workbook to the other

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    VBA copy and paste sheet from one workbook to the other

    Hi,

    I am trying to copy a sheet from one workbook to another workbook. When I debug it is opening both the workbooks and the active one is the old workbook. Please take a look and let me know where I am doing wrong. Thanks!

    here is my code:
    Sub CopyInfoSheet()
    Const Procname = "CopyInfoSheet"
    Dim StrCurrentRpt As String
    Dim strInfoPath As String
    Dim strInfofile As String
    Dim wb As Workbook

    'Get Current Report Name
    StrCurrentRpt = wb.Name

    'Open file with Lookup tables and import the 1st needed table to the current report
    strInfoPath = "\\Reporting\ValueChange\ValueChange.xlsx"
    Workbooks.Open (strInfoPath)
    strInfofile = ActiveWorkbook.Name
    Workbooks(StrCurrentRpt).Sheets("Review").Copy After:=Workbooks(strInfofile).Sheets("Info").Range("InfoRange") 'This is where I am getting error
    Workbooks(strInfofile).Close SaveChanges:=False
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Delete the Range reference;
    After:=Workbooks(strInfofile).Sheets("Info").Range("InfoRange")


    Or try this...

    Sub CopyInfoSheet()
        
        'Open file with Lookup tables and import the 1st needed table to the current report
        Application.ScreenUpdating = False
        With Workbooks.Open("\\Reporting\ValueChange\ValueChange.xlsx")
            .Sheets("Review").Copy After:=ThisWorkbook.Sheets("Info")
            .Close SaveChanges:=False
        End With
        Application.ScreenUpdating = True
        
    End Sub
    Last edited by AlphaFrog; 09-01-2015 at 02:53 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    Thank you.
    Tried to use your code and I get the below error:
    Error(438) : Object doesn't support this property or method.

    Please help. Thanks!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Quote Originally Posted by Saraaga View Post
    Thank you.
    Tried to use your code and I get the below error:
    Error(438) : Object doesn't support this property or method.

    Please help. Thanks!
    I edited the the code above. Try again with the new code.

  5. #5
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    Sorry, think I am missing something here. Where did you edit the code? Looks same to me. Thanks!

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Quote Originally Posted by Saraaga View Post
    Sorry, think I am missing something here. Where did you edit the code? Looks same to me. Thanks!
    I deleted a period before ThisWorkbook

    .Sheets("Review").Copy After:=.ThisWorkbook.Sheets("Info")

  7. #7
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    It is opening the old file where Info tab is there. Looks like it is copying too as the entire range is selected.

    however, it is not going back to the new file(it is already opened, but not activated).

    Macro is just staying at the old file and then throwing the error. Thanks!

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Are you getting the same error or is it different?
    Are you using the new code without the period?
    Is the code located in the new file?

  9. #9
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    It gives error subscript out of range.

    Yes, I am using the new without the period.

    Regarding the code, it is located in 3rd file which is macro file. I have raw data coming in from other system which is the 1st file. Macro does a lot in addition to this functionality. 2nd file which has Info tab(this is just information) has to be copied to the 1st file. Just to let you know I run the macro from the 1st file(raw data file).
    Thanks!

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Quote Originally Posted by Saraaga View Post
    It gives error subscript out of range.

    Yes, I am using the new without the period.

    Regarding the code, it is located in 3rd file which is macro file. I have raw data coming in from other system which is the 1st file. Macro does a lot in addition to this functionality. 2nd file which has Info tab(this is just information) has to be copied to the 1st file. Just to let you know I run the macro from the 1st file(raw data file).
    Thanks!

    Try this. It pastes to the original active workbook and not the workbook with that contains the code.

    Sub CopyInfoSheet()
        
        Dim wb As Workbook
        
        Set wb = ActiveWorkbook
        
        'Open file with Lookup tables and import the 1st needed table to the current report
        Application.ScreenUpdating = False
        With Workbooks.Open("\\Reporting\ValueChange\ValueChange.xlsx")
            .Sheets("Review").Copy After:=wb.Sheets("Info")
            .Close SaveChanges:=False
        End With
        Application.ScreenUpdating = True
        
    End Sub

  11. #11
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    It still gives subscript out of range.

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Double check the exact spelling of the tab names
    The Source workbook is suppose to be "Review"
    The active destination workbook is "Info"

  13. #13
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    ok, I think I know where the problem is but I don't know the solution.

    There are 2 excel files and one macro file.

    1st excel file - it has raw data and after running macro, this is the destination file too. When I get raw data, name is ValueChanges and then I am appending the name with the date of run, so final file name is ValueChanges_09012015. Running macro from this file itself. This has Review tab.

    2nd excel file has just info in it and it is located in the folder - \\Reporting\ValueChange\ValueChange.xlsx"). So everytime, I run the macro it has to open the ValueChange.xlsx and then copy "Info" sheet and paste it in the file named ValueChanges_09012015 with the same name -Info. This has to be pasted after Review tab.

    Please note that the file names are ValueChange.xlsx which has Info and the raw data file ValuesChanges_09012015

    Hope it is clear. Please help!

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    The workbook names shouldn't matter in this case.

    So you you want to copy Info sheet and paste it after Review sheet? If yes, then that's the problem.

    Your original code had it the other way around and I duplicated it. The original code copies Review and pastes it after Info.


    Try this...

    Sub CopyInfoSheet()
        
        Dim wb As Workbook
        
        Set wb = ActiveWorkbook
        
        'Open file with Lookup tables and import the 1st needed table to the current report
        Application.ScreenUpdating = False
        With Workbooks.Open("\\Reporting\ValueChange\ValueChange.xlsx")
            .Sheets("Info").Copy After:=wb.Sheets("Review")
            .Close SaveChanges:=False
        End With
        Application.ScreenUpdating = True
        
    End Sub

  15. #15
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    Thank you. However, now it throws different error.

    Run-time error '1004':
    Excel cannot insert sheets into destination workbook, because it contains fewer rows and columns than the source workbook. To move or copy the data to the destination workbook, you can select the data and then use the copy and paste commands.

  16. #16
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Can you resave the Destination workbook with the newer .xlsx file format? Then try it again.

  17. #17
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    Saved the raw file(which is the destination file), it is now throwing error:
    Application-defined or object-defined error

    .Sheets("Info").Copy After:=wb.Sheets("Review") -this is where code throwing error

    Do you think I need to specify cells or range? Thanks

  18. #18
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    Quote Originally Posted by Saraaga View Post

    Do you think I need to specify cells or range? Thanks
    No. The code copies the sheet and not a range. This should be easy.


    Confirm what file format it was saved as and what is now the raw data destination workbook's file name.

    Was the Raw destination workbook the active workbook when you started the macro?

  19. #19
    Registered User
    Join Date
    08-27-2015
    Location
    Corona
    MS-Off Ver
    2010
    Posts
    12

    Re: VBA copy and paste sheet from one workbook to the other

    Got it. I saved the info file with the extension .xls
    and then I tried to run this macro, it worked fine.

    not sure what's happening, the raw file coming from the system is in xls file, however when I am saving with the date appending, saving it as xlsx file. Anyway, it is working fine now. Thank you so much for helping me. I was going crazy.

  20. #20
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA copy and paste sheet from one workbook to the other

    You're welcome. Glad it worked out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy values from sheets in workbook and paste in one sheet in the same workbook
    By RinorM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2015, 04:14 AM
  2. Replies: 3
    Last Post: 11-06-2014, 01:23 AM
  3. copy paste from sheet and paste on same sheet of another workbook
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2014, 06:38 AM
  4. Replies: 6
    Last Post: 03-26-2014, 11:40 PM
  5. Help : Copy sheet in workbook and paste it to other workbook.
    By clonedarkman in forum Excel General
    Replies: 3
    Last Post: 03-25-2014, 04:41 AM
  6. Simple Open Workbook, Copy Sheet, Paste In New Workbook Question
    By alulla in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 02:10 PM
  7. how to copy excel sheet from one workbook and paste into another workbook, save it ?
    By dearnemo385 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2012, 08:27 AM

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.6.0 RC 1