+ Reply to Thread
Results 1 to 17 of 17

Copy data from dynamic named workbook

  1. #1
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Copy data from dynamic named workbook

    I have an open workbook "Stock Analysis.xlsm. I need a macro in it that will open another dynamic, (Cell B1 in Stock Analysis), workbook and copy data from the "Summary" worksheet and paste it into the worksheet "Summary" in the Stock Analysis workbook. Once copied then close down the dynamic worksheet.

    Cell B1 = TLS
    file to open TLS.xlsx

    I have tried fgor hours attempting this.



    Sub OpenCopyPaste()


    Workbooks.Open "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1" & i)


    ActiveWorkbook.Worksheets("Summary").Range("A1:P2").Copy


    Workbooks("Stock Analysis.xlsm").Worksheets("Summary").Range("a22:d22").PasteSpecial Paste:=xlPasteValues

    End Sub

  2. #2
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    OK, a couple of things.

    Your variable i is never defined so I don't know what is happening there. It is empty, so what is the point of adding an empty string to B1?

    [edit: forget this bit I was copying from the wrong book]

    Oh, and one other thing, you may want to put your code inside tags for a couple of reasons.

    1. It is easier to read.
    2. If you don't it upsets the moderators.

    Let me know how you go.
    Last edited by Croweater; 06-10-2021 at 10:39 PM.

  3. #3
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    This my explain better

    "Sub OpenCopyPaste()

    'open dynamic workbook
    Workbooks.Open "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1" & .xlsx)

    'activate dynamic workbook and copy rage of cells
    ActiveWorkbook.Worksheets("Summary").Range("A1:P2").Copy

    'paste copied cells from dynamic workbook into another workbook which is already open
    Workbooks.Activate("Stock Analysis.xlsm").Worksheets("Summary").Range("a22:d22").PasteSpecial Paste:=xlPasteValues

    'close dynamic workbook
    Workbooks.Close "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1" & .xlsx)



    End Sub"

  4. #4
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    Try activating your stock analysis workbook before the copy;

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    its not opening up the variable file

  6. #6
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    You are missing the quotes on ".xlsx"

  7. #7
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    Compile error

    "Sub OpenCopyPaste()

    'open dynamic workbook
    Workbooks.Open "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1" & ".xlsx"

  8. #8
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    You need to close your brackets!

    Workbooks.Open "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1") & ".xlsx"

  9. #9
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    doesnt like this line


    Workbooks.Close "C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1") & ".xlsx"

  10. #10
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    Workbooks("C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1") & ".xlsx").Close SaveChanges:=True
    Last edited by Croweater; 06-10-2021 at 11:51 PM.

  11. #11
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    Did that work? If not try this code. This is the way I'd do it.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    not liking "Set sourceBook line." the Line

    The range B1 is on worksheet "Stock A" could that be the problem

  13. #13
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    Sourcebook= Nothing ?????

  14. #14
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    got it running up to copy. its just not pasting or closing source file




    PHP Code: 
    Sub OpenCopyPaste()

    Dim sourceBook As Workbook

    'open dynamic workbook
    Set sourceBook = Workbooks.Open("C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1") & ".xlsx")

    '
    activate dynamic workbook and copy rage of cells
    sourceBook
    .Worksheets("Summary").Range("A1:P2").Copy

    'paste copied cells from dynamic workbook into another workbook which is already open
    Workbooks("StockAnalysis.xls").Activate

    ActiveWorkbook.Worksheets("Summary").Range("a22:d22").PasteSpecial Paste:=xlPasteValues

    '
    close dynamic workbook
    sourceBook
    .Close

    End Sub 

  15. #15
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy data from dynamic named workbook

    got it to work. it was not the program but my set-up

    PHP Code: 
    Sub OpenCopyPaste()

    Dim sourceBook As Workbook

    'open dynamic workbook
    Set sourceBook = Workbooks.Open("C:\Users\61458\Desktop\TEST\Windows32\data\" & Range("B1") & ".xlsx")

    '
    activate dynamic workbook and copy rage of cells
    sourceBook
    .Worksheets("Summary").Range("A1:P2").Copy

    'paste copied cells from dynamic workbook into another workbook which is already open
    Workbooks("Stock Analysis.xlsm").Activate

    ActiveWorkbook.Worksheets("Summary").Range("a22:d22").PasteSpecial Paste:=xlPasteValues

    '
    close dynamic workbook
    sourceBook
    .Close

    End Sub 

  16. #16
    Registered User
    Join Date
    06-10-2021
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    19

    Post Re: Copy data from dynamic named workbook

    Thank you Croweater and also thank you for your patience

  17. #17
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copy data from dynamic named workbook

    No worries. Glad you got it working.

+ 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. [SOLVED] Transfer dynamic named ranges from one workbook to another
    By nickersonpower in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2017, 12:14 PM
  2. Copy Data from Dynamic Web Query to Another Workbook
    By kingsdime29x in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2016, 10:26 AM
  3. [SOLVED] Clear dynamic named sheet, compare cells from 2 sheets, copy all matched rows to dynamic s
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-15-2015, 12:31 PM
  4. [SOLVED] Using a dynamic named range from another workbook
    By bob07904 in forum Excel General
    Replies: 8
    Last Post: 08-24-2015, 05:33 AM
  5. [SOLVED] Copy dynamic data from one workbook to another workbook?
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-15-2015, 10:29 AM
  6. [SOLVED] Code to copy data from a closed workbook and paste in active workbook using named range.
    By paullie1912 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2014, 02:38 AM
  7. Copy data from 2 cells into new workbook named and based on change in 3rd cell
    By imerictoo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2013, 08:57 PM

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