How to Copy Data from Another Workbook without Opening with Excel VBA: 3 Different Ways

We will use a sample Workbook named Product_Details and copy the data range (B4:E10) to another Workbook.

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA


Method 1 – Copy a Sheet Data from Another Workbook without Opening with Excel VBA

STEPS:

  • Go to the Developer tab.
  • Click on Visual Basic to open the Visual Basic Editor. (Note: You can also use keyboard shortcut  Alt + F11, or, right-click on the sheet, then select View Code)

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • Copy-paste the VBA code below to the editor.

VBA Code:

Sub Copy_Data_from_Another_Workbook()
Dim wb As Workbook
Dim newwb As Workbook
Dim rn1 As Range
Dim rn2 As Range
Set wb = Application.ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
        Application.Workbooks.Open .SelectedItems(1)
        Set newwb = Application.ActiveWorkbook
        Set rn1 = Application.InputBox(prompt:="Select Data Range", Default:="A1", Type:=8)
        wb.Activate
        Set rn2 = Application.InputBox(prompt:="Select Destination Range", Default:="A1", Type:=8)
        rn1.Copy rn2
        rn2.CurrentRegion.EntireColumn.AutoFit
        newwb.Close False
    End If
End With
End Sub
End With
End Sub
  • Click the Run Sub button, or press the keyboard shortcut F5, to run the code.

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • The File Open window will pop up.
  • Select the workbook you want to collect data from.
  • Click on the OK button.

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • Select the data from the source file by dragging over the desired range (example, B5:E10) and click OK.

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • Select the destination range where you want to put the data.
  • Click OK.

  • The source file will be closed and the data will be copied to the destination file.

Read More: Excel VBA: Copy Range to Another Workbook


Method 2 – VBA to Copy a Data Range from Another Workbook without Opening in Excel

STEPS:

  • Go to the Developer tab.
  • Open the Visual Basic Editor by clicking on Visual Basic (or by pressing Alt + F11, or right-clicking on the sheet and selecting View Code).

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • Copy-paste the VBA code below.

VBA Code:

Sub CollectData()
Dim rgTarget As Range
Set rgTarget = ActiveSheet.Range("B2:E10") 'where to put the copied data.
rgTarget.FormulaArray = "='D:\[Product_Details.xlsx]Sheet1'!$B$4:$E$10"
rgTarget.Formula = rgTarget.Value
End Sub
  • Click Run Sub or press the keyboard shortcut F5 to run the code.

NOTE: You need to change the range in the code as per your source data.

  • The data will be copied into the active workbook.

Read More: Macro to Copy and Paste from One Worksheet to Another


Method 3 – Excel VBA to Copy Data from Another Workbook without Opening by Using Command Button

STEPS:

  • Go to the Developer tab.
  • Click on the Insert drop-down menu.
  • Click on the Command Button.

  • Put the source file sheet name (example, Product) on cell A1. Set the Command Button on the right side of the source file sheet name. Create the table required for the data.

  • Go to the Developer.
  • Click on Visual Basic to launch the Visual Basic Editor.

3 Different Ways to Copy Data from Another Workbook without Opening with Excel VBA

  • Copy-paste the VBA code below.

VBA Code:

Private Sub CommandButton1_Click()
Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Set currentworkbook = ThisWorkbook
Set sourceworkbook = Workbooks.Open("D:\Products_Details.xlsx")
sourceworkbook.Worksheets("Product").Range("B5:E10").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet3").Activate
currentworkbook.Worksheets("Sheet3").Cells(4, 1).Select
ActiveSheet.Paste
sourceworkbook.Close
Set sourceworkbook = Nothing
Set currentworkbook = Nothing
ThisWorkbook.Activate
Worksheets("Sheet3").Activate
Worksheets("Sheet3").Range("A2").Select
End Sub
  • Press Ctrl + S to save the code.

NOTE: You need to change the file path and the data range in the code.

  • Click on the CommandButton1 to copy the data from another workbook without opening it.

Read More: Macro to Copy Data from One Workbook to Another Based on Criteria


Download Practice Workbook

You can download the workbook and practice with them.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sabrina Ayon
Sabrina Ayon

Sabrina Ayon, a Computer Science and Engineering graduate from United International University, has been an integral part of the ExcelDemy project for two years. She authored 150+ articles, excelling in instructing through visually engaging Excel tutorials. With a passion for teaching, Sabrina conducted sessions on Excel VBA, sharing her knowledge and insights with others. Currently holding the position of Project Manager for the ExcelDemy Visual Development Project, she oversees various aspects of the project, ensuring its smooth operation... Read Full Bio

4 Comments
  1. This has been really helpful, thank you.
    I used version: 1. Copy a Sheet Data from Another Workbook without Opening with Excel VBA, and its worked brilliantly, I do have one questions which is can the code be altered so that it pastes the values rather than the formatting of the cell?

    For example, when I use this code it pastes the drop down data validation from the workbook to the destination workbook, but I want to keep the validation as is on the destination workbook.

    Hope this makes sense

    • Hi PETE,

      Thanks for your comment. I am replying on behalf of Exceldemy. To paste the values, you can try the code below:

      Sub Copy_Data_from_Another_Workbook()
      Dim wb As Workbook
      Dim newwb As Workbook
      Dim rn1 As Range
      Dim rn2 As Range
      Set wb = Application.ActiveWorkbook
      With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
      Application.Workbooks.Open .SelectedItems(1)
      Set newwb = Application.ActiveWorkbook
      Set rn1 = Application.InputBox(prompt:="Select Data Range", Default:="A1", Type:=8)
      wb.Activate
      Set rn2 = Application.InputBox(prompt:="Select Destination Range", Default:="A1", Type:=8)
      rn1.Copy
      rn2.PasteSpecial xlPasteValues
      newwb.Close False
      End If
      End With
      End Sub

      Here, we have used the code of Method 1. We changed the highlighted lines to copy and paste only the values.
      I hope this will help you solve your problem. Please let us know if you have any other queries.
      Thanks!

  2. just to the point
    thanks

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo