+ Reply to Thread
Results 1 to 17 of 17

Copy all data from an Excel sheet in a different workbook to a blank sheet in current WB

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Copy all data from an Excel sheet in a different workbook to a blank sheet in current WB

    Not sure why I'm finding this so hard....

    Using VBA, I want to be able to import a whole sheet from another Workbook into my current spreadsheet. I know the path, filename and worksheet name of the target data.

    I don't know how many rows / columns there are in the table in the target sheet so its fine to copy the whole sheet into a nominated sheet in my current Workbook. All examples I've found on Google assume you know the columns / rows of the target, or prompt the user to type that in. I just want ALL data in the target sheet as it varies.

    Trying something like this but not able to do the import...

    'Open the target spreadsheet
    Set wbkImportData = Workbooks.Open(strTargetWorkbook)
    wbkImportData.Sheets(strTargetSheet).Select
    wbkImportData.ActiveSheet.Cells.Select
    wbkImportData.Selection.Copy <------------ This line throws up an error

    '### This next bit of code just doesn't work even if I manually (using ctrl+c) copy the target data to the clipboard
    'In my current workbook, select a blank sheet to import the data
    Sheets("ImportData").Select
    Cells.Select
    ActiveSheet.Paste

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Hi drjarmin

    Not sure what your setup or actual filenames are but this will get you started...
    Option Explicit
    
    Sub GetStuff()
    'Declare Variables
    Dim wb1 As Workbook, wb2 As Workbook, ws As Worksheet
    'Assign ref of ActiveWorkbook housing the code
    Set wb1 = ThisWorkbook: Set ws = wb1.ActiveSheet
    'Assign ref of Workbook to open
    Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\" & "Tester.xlsx") 'Change Tester.xlsx to name of your workbook.
    'Copy the information required
    ActiveSheet.UsedRange.Copy
    'Paste into ActiveWorkbook housing the code
    ws.Activate
    Range("A1").PasteSpecial xlPasteValues
    'Clear Clipboard
    Application.CutCopyMode = False
    End Sub
    Last edited by Sintek; 12-26-2017 at 06:03 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Thank you, that worked a treat. 'UsedRange' was the solution.

    Is there any way I can select the active range (the data I've selected) in the target workbook? Now I have 'UsedRange', I can apply a filter in the Target sheet (based on a value in column 1 of the table) and only copy that data rather than copying the whole sheet. Problem is I get an error trying to copy the data....

            Set wbkImportData = Workbooks.Open(Filename:=strImportPath & strImportFileName, UpdateLinks:=0)
            'Apply a filter to the data to select all rows with column 1 set to "ACME LTD"
            wbkImportData.ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="ACME LTD"
            
            'Next two lines to select all the filtered data (<----------is there an easier way to select the results of the filter?)
            wbkImportData.ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
            wbkImportData.ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
            
            'Copy the selected data to paste back into the main workbook running the code
            wbkImportData.Selection.Copy   '<----------This line (and all other variants I've tried) throws up an 438 error. If I record a macro at this point, it uses selection.copy
    
            'return to main workbook and paste the data
            wbkThisWorkbook.Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False 'Stop prompt of large amount of data saved to clipboard....
    
            'Close workbook where the data came from
            Workbooks(strImportFileName).Close savechanges:=False
    Last edited by drjarmin; 12-26-2017 at 06:29 AM.

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Not sure I follow....What Range do you want to copy and where do you want to paste it...

    Edit
    is there an easier way to select the results of the filter?
    .SpecialCells(12).Copy
    Perhaps you should upload a sample

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    ....Delete.....
    Last edited by Sintek; 12-26-2017 at 06:41 AM.

  6. #6
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    My target data is in multiple tables spread across many workbooks so I'm creating a loop to open each spreadsheet, copy the data and paste into its own sheet in my workbook. I've got this working since you posted to use the UsedRange range object.

    But I don't need ALL the data in each table. I only need 1 or more rows which is based on a value in column 1. This is the same for every sheet I need to import. For examples sake, let's say this column is called 'Supplier Name' and I only want rows where the supplier is called 'ACME LTD'.

    The attached diagram shows how the spreadsheet with the the data to be imported looks at each step of the process.

    Step 1

    I've modified my code so it opens up each target sheet

    and applies an auto filter on the sheet and filters on column 1. So if I only wanted to import rows from 'Supplier Name' = 'ACME LTD' I run this code...

    Step 2

    wbkImportData.ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="ACME LTD"
    Step 3

    After the filter is applied, I now have a table showing just the rows I want to copy and paste into my workbook as the auto filter will hide rows that don't meet the criteria. So my logic is I need to select all the 'visible' rows (use CTRL+Shift+Down, CTRL+Shift+End)...

     wbkImportData.ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    wbkImportData.ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
    Step 4 - Here is where I get stuck

    When I run this, all the data I want is selected. I just need to copy this back into my main workbook. But the 'copy' method throws up an error

    wbkImportData.Selection.Copy
    <--------- I can't 'select' the data to paste it back without an error. This is the only line of code that doesn't work

    If I can copy this data, I am set as my next code block activates my main workbook correctly and pastes it where I want it.

    wbkThisWorkbook.Activate
    ActiveSheet.Paste
    Example.jpg
    Last edited by drjarmin; 12-26-2017 at 07:05 AM.

  7. #7
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    *Deleted* Managed to combine image with post above.
    Last edited by drjarmin; 12-26-2017 at 07:06 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Pls upload a sample workbook and I will gladly help solve your problem...

    Edited...This is what you need to achieve....Tx to bakerman2
    With wbkImportData
        With .ActiveSheet.Cells(1).CurrentRegion
            .AutoFilter Field:=1, Criteria1:="ACME LTD"
            .SpecialCells(12).Copy wbkThisWorkbook.ActiveSheet.Range("A1")
        End With
        ActiveSheet.AutoFilterMode = False
    End With
    Last edited by Sintek; 12-26-2017 at 07:33 AM.

  9. #9
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Here is an example

    1. 'Run Example Macro in this Workbook.xlsm'

    This contains a module with one macro called 'Example'

    2. 'ImportData.xlsx'

    This is the data to import. Path for this file is hard coded as 'C:\temp\' in the macro above
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Aiya, very easy when you see the solution! Thank you, perfect.

    .ActiveSheet.Cells(1).CurrentRegion & .SpecialCells(12).Copy and its all working.

    Really appreciate the help!

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Here you go...
    Option Explicit
    
    Private Sub CommandButton1_Click()
    Dim wbkThisWorkbook As Workbook, wbkImportData As Workbook
    Dim ws As Worksheet
    Dim strImportPath As String, strImportFileName As String
    strImportPath = ThisWorkbook.Path
    strImportFileName = "ImportData.xlsx"
    Set wbkThisWorkbook = ThisWorkbook: Set ws = wbkThisWorkbook.ActiveSheet
    Set wbkImportData = Workbooks.Open(Filename:=strImportPath & "\" & strImportFileName, UpdateLinks:=0)
    With wbkImportData
        With .ActiveSheet.Cells(1).CurrentRegion
            .AutoFilter Field:=1, Criteria1:="ACME LTD"
            .SpecialCells(12).Copy wbkThisWorkbook.ActiveSheet.Range("A1")
        End With
        ActiveSheet.AutoFilterMode = False
    End With
    Workbooks(strImportFileName).Close savechanges:=False
    Application.CutCopyMode = False
    End Sub
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    For my understanding, why doesn't 'selection.copy' work? That is the code the macro recorder generates.

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    It does work ... few examples...Glad i could help...Tx for rep +
    Sub Selection_Copy()
    Selection.Copy Sheet2.Range("A1")
    End Sub
    Sub Selection_Copy()
    Selection.Copy
    Sheet2.Range("A1").PasteSpecial xlPasteValues
    End Sub
    Sub Selection_Copy()
    Selection.Copy Destination:=Sheet2.Range("A1")
    End Sub
    Last edited by Sintek; 12-26-2017 at 08:36 AM.

  14. #14
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Ah, ok, so I guess my original code wasn't working because I didn't specify a destination to the copy method.

    Still trying to get my head around this. Trying some other examples but not working. How would I modify this existing code, for example, copy all of column A with data in it (A1.....A100 or whatever the last cell with data is)?

    I guess I can't use the .SpecialCells(12) property as this would select all the data and I only want column A. Also, I don't want to filter it as I want all values....

    With wbkImportData
        With .ActiveSheet.Cells(1).CurrentRegion
            .AutoFilter Field:=1, Criteria1:="ACME LTD"
            .SpecialCells(12).Copy wbkThisWorkbook.ActiveSheet.Range("A1")
        End With
        ActiveSheet.AutoFilterMode = False
    End With
    Trying things like this but they just generate an Err=91 error:

    wbkImportData.Sheets("Sheet1").Range("A1:A100").Copy wbkThisWorkbook.ActiveSheet.Range("A1")
    Last edited by drjarmin; 12-26-2017 at 09:45 AM.

  15. #15
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Your code does work...
    wbkImportData.Sheets("Sheet1").Range("A1:A100").Copy wbkThisWorkbook.ActiveSheet.Range("A1")
    Try this
    With wbkImportData
        With .ActiveSheet
            .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row).Copy wbkThisWorkbook.ActiveSheet.Range("A1")
        End With
    End With
    Last edited by Sintek; 12-26-2017 at 10:46 AM.

  16. #16
    Registered User
    Join Date
    10-29-2017
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Yes that works! Thanks again.

    Now, the problem is the data is numbers being stored as text so when I sort them I get odd results (and those little green triangle error boxes on the top left corner). Going to try figure that out or start a new thread.

  17. #17
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,606

    Re: Copy all data from an Excel sheet in a different workbook to a blank sheet in current

    Best to start a new thread as your original query has been solved

+ 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] vba copy dynamic data from sheet 1 to sheet 2 with blank cells
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-03-2017, 11:21 AM
  2. [SOLVED] Create macro to copy current sheet to new folder and with new name of workbook
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-29-2016, 02:21 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. Create new Excel sheet containing data from current sheet.
    By andy.allard in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2014, 06:02 AM
  5. how to copy columns of another sheet with the column data of current sheet
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2014, 11:28 PM
  6. Excel vba to copy sheet from current workbook to new Excel sheet ?
    By dearnemo385 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2012, 03:27 PM
  7. Command button - to copy active sheet and not save current workbook
    By vjboaz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2008, 05:23 PM

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