+ Reply to Thread
Results 1 to 8 of 8

Get data from Closed workbook and paste in current Workbook

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Get data from Closed workbook and paste in current Workbook

    Hello:

    I have closed workbook with some data.
    The name of the sheet where the data is will be the same as name of the closed workbook.
    Example : Name of the closed workbook is Store1Data.xls
    Then, the name of the sheet in closed workbook where the data is will be "Store1Data"

    I need VB Code to copy closed worklook data in Sheet "ImportData" of the current workbook.

    Name and Location of the closed upbook will be in Cell S3 of the current workbook.

    Please let me know if you have any questions.
    Thanks.

    RM
    Last edited by rizmomin; 10-08-2015 at 10:43 AM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,882

    Re: Get data from Closed workbook and paste in current Workbook

    Hi RM. Place this macro in a regular module in your current workbook and run it from there:
    Sub CopyData()
        Application.ScreenUpdating = False
        Dim wkbDest As Workbook
        Dim wkbSource As Workbook
        Set wkbDest = ThisWorkbook
        Dim LastRow As Long
        Set wkbSource = Workbooks.Open(Range("S3"))
        With wkbSource
            .Sheets("Store1Data").UsedRange.Copy wkbDest.Sheets("ImportData").Cells(1, 1)
            .Close savechanges:=False
        End With
        Application.ScreenUpdating = True
    End Sub
    You didn't specify exactly what data you wanted to copy so this macro copies all the data in the sheet.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: Get data from Closed workbook and paste in current Workbook

    Hi Mumps1:

    Seems to work to the point that the closed file will open and it will give error at

    .Sheets("Store1Data").UsedRange.Copy wkbDest.Sheets("ImportData").Cells(1, 1)
    I have added The sheet name in current workbook at cell T2
    and now i have changed above line to
     Set wkbSource = Workbooks.Open(Range("S3"))
        wSheet = Range("T2").Value
        
        With wkbSource
            '.Sheets("Store1Data").UsedRange.Copy wkbDest.Sheets("ImpEmp").Cells(1, 1)
            .Sheets(wSheet).UsedRange.Copy wkbDest.Sheets("ImpEmp").Cells(1, 1)
            .Close savechanges:=False
        End With
        Application.ScreenUpdating = True
    Still does not copy and close

    Please have a look and let me know.
    Thanks
    RM

  4. #4
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: Get data from Closed workbook and paste in current Workbook

    Hi Mumps1:

    Seems to work to the point that the closed file will open and it will give error at

    .Sheets("Store1Data").UsedRange.Copy wkbDest.Sheets("ImportData").Cells(1, 1)
    I have added The sheet name in current workbook at cell T2
    and now i have changed above line to
     Set wkbSource = Workbooks.Open(Range("S3"))
        wSheet = Range("T2").Value
        
        With wkbSource
            '.Sheets("Store1Data").UsedRange.Copy wkbDest.Sheets("ImpEmp").Cells(1, 1)
            .Sheets(wSheet).UsedRange.Copy wkbDest.Sheets("ImpEmp").Cells(1, 1)
            .Close savechanges:=False
        End With
        Application.ScreenUpdating = True
    Still does not copy and close

    Please have a look and let me know.
    Thanks
    RM

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,882

    Re: Get data from Closed workbook and paste in current Workbook

    When I tried the code I posted in Post#2 on some dummy data and workbooks, it worked properly. It would be easier to test if I could see your actual files. Could you post a copy of each file?

  6. #6
    Registered User
    Join Date
    02-09-2014
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Get data from Closed workbook and paste in current Workbook

    I use this all the time to get the data from a closed file


    Function GetDataFromClosed(Path, File, Sheet, Address)
    Dim strAddress As String
    Dim cl
        
        strAddress = Path & File & Sheet
        Worksheets(Sheet).UsedRange.Clear
        
        With Worksheets(Sheet).Range(Address)
        
            On Error Resume Next
        
            .FormulaR1C1 = "=IF('" & strAddress & "'!RC="""",NA(),'" & strAddress & "'!RC)"
            
            .SpecialCells(xlCellTypeFormulas, xlErrors).Clear
            
            On Error GoTo 0
            
            .Value = .Value
            
            
        End With
        
        
    End Function
    
    Sub Call_Get_Data()
    Dim strTemp As String
    Dim strName As String
    
    
            strTemp = "A1:H10000" ''range of cells to copy from/to (change as required)
            strName = "[" & "workbook_to_load_from.xls" & "]"
            strTemp = GetDataFromClosed("C:\Temp\", strName, "WorkSheet_To_Copy_From", strTemp)
                
            ActiveSheet.Cells.Range("A1:H10000").Font.Bold = False''this tidies up the worksheet after the import function
            Columns.AutoFit
    
    
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: Get data from Closed workbook and paste in current Workbook

    Hi Mumps1:

    I got it fugre out, I think, I will test further and let you know if any problem.
    Thanks a lot for great hep.
    RM

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,882

    Re: Get data from Closed workbook and paste in current Workbook

    Glad to help.

+ 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] Copy data from activeworkbook paste to closed workbook
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-02-2014, 10:16 AM
  2. Replies: 0
    Last Post: 03-27-2014, 12:38 PM
  3. VBA code for copy data from closed workbook to "Data " sheet of current workbook
    By satputenandkumar0 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-13-2014, 02:23 AM
  4. [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
  5. vba vlookup updates current data's from closed workbook undefined name of workbooks
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2013, 06:53 AM
  6. Filter data on seperate workbook and paste on current workbook
    By ZeDoctor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2012, 08:08 AM
  7. Loop through data on a workbook, selected it and paste in current workbook
    By concatch in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2012, 08:59 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