VBA Solution Required - Copy data from a closed workbook, paste into sheet in active workbook, delete closed workbook

michealhill

New Member
Joined
Jul 1, 2019
Messages
16
Hi,

I'm looking for a code that can be run from a reporting workbook that I am building, which will copy data from a closed workbook, paste into a sheet in my reporting workbook and then delete the source file for the copied data once complete.

Some additional detail that might help:
  • the closed workbook will be called Batch Export, it will have one sheet called Summary
  • the closed workbook will be located in the same network folder location as the reporting workbook
  • the sheet on the reporting workbook will be called Batch Export
Is anyone able to help with a solution? Any assistance would be greatly appreciated!

Thanks in advance,

Micheal
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Do you want to copy the entire "Summary" sheet or only part of it? If only part, which range(s)?
 
Upvote 0
Place this macro in a regular module in your reporting workbook and run it from there. Before running it, make sure you have a backup copy of the Batch Export workbook in a different folder just in case. Change the file extension (in red) to suit your needs.
Rich (BB code):
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet
    Set desWS = ThisWorkbook.Sheets("Batch Export")
    Workbooks.Open ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Sheets("Summary").UsedRange.Copy desWS.Cells(1, 1)
    ActiveWorkbook.Close False
    Kill ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Place this macro in a regular module in your reporting workbook and run it from there. Before running it, make sure you have a backup copy of the Batch Export workbook in a different folder just in case. Change the file extension (in red) to suit your needs.
Rich (BB code):
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet
    Set desWS = ThisWorkbook.Sheets("Batch Export")
    Workbooks.Open ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Sheets("Summary").UsedRange.Copy desWS.Cells(1, 1)
    ActiveWorkbook.Close False
    Kill ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Application.ScreenUpdating = True
End Sub
Thanks Mumps. I will try this on Monday and let you know how it went
 
Upvote 0
Place this macro in a regular module in your reporting workbook and run it from there. Before running it, make sure you have a backup copy of the Batch Export workbook in a different folder just in case. Change the file extension (in red) to suit your needs.
Rich (BB code):
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet
    Set desWS = ThisWorkbook.Sheets("Batch Export")
    Workbooks.Open ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Sheets("Summary").UsedRange.Copy desWS.Cells(1, 1)
    ActiveWorkbook.Close False
    Kill ThisWorkbook.Path & "\" & "Batch Export.xlsx"
    Application.ScreenUpdating = True
End Sub

hi mumps. i tried your code but got a subscript out of range error.

i have manage to piece together a code that somewhat works, however not fully as intended. see below:

VBA Code:
Sub ImportBatch()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim wbExp As Workbook
Dim wbRep As Workbook
Dim wsSumm As Worksheet
Dim wsBatch As Worksheet

Set wbRep = ThisWorkbook
Set wsBatch = wbRep.Sheets("Batch Export")
Set wbExp = Workbooks.Open("R:\Data Services\KPI Reports\Batch Export.xlsx")
Set wsSumm = wbExp.Sheets("Summary")

wsSumm.Copy wsBatch

wbExp.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

However, the code above is pasting the copied data as a new sheet in the destination workbook, rather than in the wsSumm sheet specified.

Is anyone able to provide some guidance on how i can paste it into the designated sheet (ideally i would also like it to paste special as values)

Thanks in advance!
 
Upvote 0
When you get the error, which line of code is highlighted when you click "Debug"?
 
Upvote 0
When you get the error, which line of code is highlighted when you click "Debug"?

the line with the error was:

VBA Code:
Set desWS = ThisWorkbook.Sheets("Batch Export")


i have now got a code that works almost as intended:

VBA Code:
Sub ImportBatch()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim wbExp As Workbook
Dim wbRep As Workbook
Dim wsSumm As Worksheet
Dim wsBatch As Worksheet

Set wbRep = ThisWorkbook
Set wsBatch = wbRep.Sheets("Batch Export LI")
Set wbExp = Workbooks.Open("R:\Data Services\KPI Reports\Batch Export LI.xlsx")
Set wsSumm = wbExp.Sheets("Summary")

wsBatch.Range("B:S").Value2 = wsSumm.Range("A:R").Value2
wbExp.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

the only amendments i now need are:

the range being copied will be a set column range but variable row range, i need the code to be able to import the range that is being used
i also need the source worksheet to be deleted upon completion

any suggestions?
 
Upvote 0
Try this macro. I have moved the Application.DisplayAlerts lines to prevent a pop-up when the Summary sheet is deleted. I have also added "True" to the line that closes the workbook so that it is saved.
VBA Code:
Sub ImportBatch()
    Application.ScreenUpdating = False
    Dim wbExp As Workbook, wbRep As Workbook, wsSumm As Worksheet, wsBatch As Worksheet, LastRow As Long
    Set wbRep = ThisWorkbook
    Set wsBatch = wbRep.Sheets("Batch Export LI")
    Set wbExp = Workbooks.Open("R:\Data Services\KPI Reports\Batch Export LI.xlsx")
    Set wsSumm = wbExp.Sheets("Summary")
    With wsSumm
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        wsBatch.Range("B1:S" & LastRow).Value2 = .Range("A1:R" & LastRow).Value2
        Application.DisplayAlerts = False
        .Delete
        Application.DisplayAlerts = True
    End With
    wbExp.Close True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,679
Messages
6,126,186
Members
449,296
Latest member
tinneytwin

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