+ Reply to Thread
Results 1 to 7 of 7

Thread: Copy from an non-activated workbook and paste into the active one

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    Ghent, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Copy from an non-activated workbook and paste into the active one

    Hello,

    I am trying to copy some values from a workbook that is not open and paste them into the active workbook that contains the macro. Though it sounds relatively easy, I cannot do it.

    The problem seems to be when pasting the values, at the last line of my code. I keep receiving the following message : "Run time error 438 Object doesn't support this property or method".

    The code that I have written is the following:

    Sub copy_service_scores()
    '
    ' 
    
    Dim s_file As String 'defintion of the source file variable
    Dim t_file As String 'defintion of the target file variable
    
    s_file = "full path.xls"
    
        Workbooks.Open s_file
        Sheets("sheet name").Select
        Range("D10:E37").Select
        Selection.Copy
        
        Windows("the name of the workbook that is already open and contains the macro .xlsx").Activate
        Sheets("Sheet1").Select
        Range("D10").Select
        Selection.Paste
    
    End Sub
    Do I need to change the entire structure or could I do sth simpler to make that work?

    Thanks and regards,
    Ilias
    Last edited by Ilias1987; 02-05-2012 at 05:03 PM. Reason: add code tags PM rules

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Copy from an non-activated workbook and paste into the active one

    You could use
    Sub test()
    Set wb = ActiveWorkbook.Worksheets("Sheet1") ' This the workbook which is open where you want to paste 
    Workbooks.Open("c:\ttt\Book1.xlsx").Worksheets("Sheet1").Range("D10:E37").Copy ' change to the path of the book you want to copy from
    wb.Range("D10").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
    End Sub
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Registered User
    Join Date
    02-04-2012
    Location
    Ghent, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy from an non-activated workbook and paste into the active one

    Thanks for the tip smuzoen!

    Now the problem is that I have 100 different closed book (let us say book_1 ... book_100) and I want to copy the data from an identical sheet in each of these workbooks (let us say that the name of this sheet will be always common_sheet). This data will be pasted in 100 different sheets inside the active workbook.

    The process I had in mind was

    For x = 1 to 100

    copy data from book_x common_sheet
    paste data into active_workbook sheet_x

    Next x
    However, now I don't know how to combine that logic when using the path to open each closed sheet.

    Any ideas please??

    Thanks in advance

    Ilias

  4. #4
    Registered User
    Join Date
    02-04-2012
    Location
    Ghent, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy from an non-activated workbook and paste into the active one

    I found a way to do it, by the following code
    Sub copy_paste ()

    For x = 1 To 100

    target_ws = "Sheet" & x
    source_wb = "file path" & x & ".xls"

    Set wb = ActiveWorkbook.Worksheets(target_ws)
    Workbooks.Open(source_wb).Worksheets("Serv").Range("B2:K37").Copy
    wb.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

    Set wb2 = ActiveWorkbook
    wb2.Close SaveChanges = False


    Next x

    End Sub
    Now I am getting this message

    "There is a large amount of information on the clipboard. Do you want to paste this information into another program later... "

    at the end of each loop, when reaching
    wb2.Close SaveChanges = False
    Any additional code to get rid of that message while running the macro? I don't want to have this data available later!

    Kr
    Ilias

  5. #5
    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 from an non-activated workbook and paste into the active one

    Hello Ilias1987,

    I have added a line to the macro that will clear the clipboard after the workbook is closed.
    Sub copy_paste ()
    
    For x = 1 To 100
    
    target_ws = "Sheet" & x
    source_wb = "file path" & x & ".xls"
    
    Set wb = ActiveWorkbook.Worksheets(target_ws)
    Workbooks.Open(source_wb).Worksheets("Serv").Range("B2:K37").Copy
    wb.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    
    Application.CutCopyMode = False
    Set wb2 = ActiveWorkbook
    wb2.Close SaveChanges = False
    
    Next x
    
    End Sub
    Last edited by Leith Ross; 02-05-2012 at 04:15 PM.
    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!)

  6. #6
    Registered User
    Join Date
    02-04-2012
    Location
    Ghent, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy from an non-activated workbook and paste into the active one

    Thanks again,

    How can I marked it as solved? Is that an option in some sort of menu or just type [SOLVED] in a new post? Sorry for asking, I m new to the forum

  7. #7
    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 from an non-activated workbook and paste into the active one

    Hello Ilias1987,

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    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!)

+ 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