Macro to open specific Workbook and copy data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to open a file and copy data from sheet "Summary Group" but no data is pasted into the current workbook

It would be appreciated if someone could assist me

Code:
 Sub Open_Imported_Data()
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.CutCopyMode = False
End With


ChDir "C:\Sales Reports"

Application.ScreenUpdating = False
Workbooks.Open Filename:= _
"C:\Sales Reports\BR1 Group Sales Ver 1.1.xlsm"
Sheets(3).Range("A1:H" & Rows.Count).End(xlUp).Copy
Sheets("Imported Data").PasteSpecial Paste:=xlPasteValues
Sheets("Imported Data").PasteSpecial Paste:=xlPasteFormats



With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.CutCopyMode = False
End With
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The issue is that once you open up the new workbook, that becomes the active workbook. So you need to select/activate the first workbook again so you can paste the data to it.
Whenever I am doing something like this, I like to use 2 workbook variables. Set the first one right away in your code, and the second one right after you open the new workbook.
Now you have a good way to differeniate the two workbooks and can easily move back and forth between them.

So I would update your code like this:
VBA Code:
Sub Open_Imported_Data()

    Dim wb1 As Workbook
    Dim wb2 As Workbook
    
'   Set current workbook to wb1
    Set wb1 = ActiveWorkbook

    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .CutCopyMode = False
    End With

    ChDir "C:\Sales Reports"

    Workbooks.Open Filename:= _
        "C:\Sales Reports\BR1 Group Sales Ver 1.1.xlsm"
        
'   Set new workbook just opened up to wb2
    Set wb2 = ActiveWorkbook
        
    Sheets(3).Range("A1:H" & Rows.Count).End(xlUp).Copy

'   Activate wb1 to paste to
    wb1.Activate
    Sheets("Imported Data").PasteSpecial Paste:=xlPasteValues
    Sheets("Imported Data").PasteSpecial Paste:=xlPasteFormats

    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
        .CutCopyMode = False
    End With
    
End Sub
So you can see you can easily activate one of the workbooks with a command like:
VBA Code:
wb1.Activate
You can also easily close the workbook you just opened like this:
VBA Code:
wb2.Close
 
Upvote 0
Try this

VBA Code:
Sub Open_Imported_Data()
  With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .CutCopyMode = False
  End With
  
  Workbooks.Open Filename:="C:\Sales Reports\BR1 Group Sales Ver 1.1.xlsm"
  Sheets(3).Range("A1:H" & Sheets(3).Range("A" & Rows.Count).End(xlUp).Row).Copy
  With ThisWorkbook.Sheets("Imported Data").Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
  End With
  
  With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .CutCopyMode = False
  End With
End Sub
 
Upvote 0
Thanks for help and your valuable input

I now get a run time error "application defined or object-defined error" and the code below is highlighted


Code:
 Sheets("Imported Data").PasteSpecial Paste:=xlPasteValues
 
Upvote 0
I was assuming that the rest of your code was good, but it looks like that may not be the case.
Did you try Dante's code? It looks like he re-wrote those sections of your code.
 
Upvote 0
I now get a run time error "application defined or object-defined error" and the code below is highlighted

Did you try Dante's code? It looks like he re-wrote those sections of your code.

Thanks Joe, so I checked all the code and found several details that I corrected in post #3.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top