+ Reply to Thread
Results 1 to 9 of 9

copy & paste between workbooks

  1. #1
    Registered User
    Join Date
    11-19-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    57

    copy & paste between workbooks

    Hi All,
    I’m stuck with a problem that I’m sure is very simple to solve, but my brain is just not working today.
    I need to copy a range from workbook A, paste it in workbook B, then copy another range from workbook B and paste it in workbook A.
    Workbook B always has the same file and sheet name, but for Workbook A, the file name and sheet name are always different.

    This is my code:

    Sub Macro1()

    Dim myWb As Workbook
    Dim mySheetName As String

    mySheetName = ActiveSheet.Name

    Range("A:G"). Copy
    Workbooks("Personal.xlsm"). Sheets(2).Activate ‘ this always the same workbook and sheet
    Range("A1").Select
    ActiveSheet.Paste

    …Rest of my code, and then …

    Range("H:H").Copy

    and now, how do I go back to Workbook A to paste it there????

    i though something like the below should work but it doesn't

    ActiveWorkbook.Worksheets(mySheetName).Activate
    Range("H1").Select
    ActiveSheet.Paste

    please?

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: copy & paste between workbooks

    Please Login or Register  to view this content.
    Elegant Simplicity............. Not Always

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: copy & paste between workbooks

    Hi, dOOb,

    maybe just set objects and use them? I assume that workobok personal.xlsm is open (and I would definitely have a different name as not to interfere with the personal macro book):

    Please Login or Register  to view this content.
    Instead of copying whole columns you might like to experiment with an intersect of UsedRange and these columns.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    11-19-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    57

    Re: copy & paste between workbooks

    Thanks a lot for your replies,

    Andy, the solution you offered pastes the values from column H in Workbook B (copies&pastes over itself) and not A where i need it - i tried something similar already before, with no result - is it possible something is wrong with my excel?

    Holger, your solution causes a Run Time error (Object required) in line:

    msheets.Range("H:H").Value = .Range("H:H").Value

    - i don't understand what's "msheets" - (should be "sheets" maybe?)

    thank you both for that, but the problem is still unsolved

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: copy & paste between workbooks

    Hi, dOOb,

    my fault as I hadn´t tested the code, it should read as you expected
    Please Login or Register  to view this content.
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    11-19-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    57

    Re: copy & paste between workbooks

    Holger, thank you, i've corrected that (and the typo in "Range"), but unfortunately it still does not do what i need - i don't understand where's the issue

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: copy & paste between workbooks

    Hi, dOOb,

    (and the typo in "Range"
    If you mean the dot preceeding Ranges you should leave them in place as this is within a With-Statement where the name of the workbook and the worksheet are being referenced.

    Written in long it should look like this:
    Please Login or Register  to view this content.
    which was shortend to read
    Please Login or Register  to view this content.
    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    11-19-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    57

    Re: copy & paste between workbooks

    no, i meant the Ramge instead of Range in:
    .Range("A:G").Value = mySheet.Ramge("A:G").Value

    but still, with that changed it does not work

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: copy & paste between workbooks

    Hi, dOOb,

    oops - you´re right about that one. I build a quick working model but I couldn´t encounter any problems or run-time errors when running the corrected code (exccept for the extra time as only 35 rows were filled but the whole columns were copied).

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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