Andyatwork
Board Regular
- Joined
- Mar 29, 2010
- Messages
- 94
Hi all,
I have some working code that accomplishes the task but I am sure there is a more elegant solution that someone might be able to suggest.
The goal is to enable a button linked to the macro so the user can click it, pick a file (an xls report of unknown name with one sheet only), and have the contents of the picked file copied to the second sheet which will then be renamed.
This is a precursor to pivoting that data and squirting the pivot results out to a new workbook.
Is there a way of doing the sheet copy without opening the target workbook?
I've been experimenting with filedialog and application.getopenfilename but keep getting compile errors.
My code:
I have some working code that accomplishes the task but I am sure there is a more elegant solution that someone might be able to suggest.
The goal is to enable a button linked to the macro so the user can click it, pick a file (an xls report of unknown name with one sheet only), and have the contents of the picked file copied to the second sheet which will then be renamed.
This is a precursor to pivoting that data and squirting the pivot results out to a new workbook.
Is there a way of doing the sheet copy without opening the target workbook?
I've been experimenting with filedialog and application.getopenfilename but keep getting compile errors.
My code:
Code:
Option Explicit
'wbExt is the source file, wbRpt is the pivot report macrobook
Dim wbExt As Workbook
Dim wbRpt As Workbook
Sub Copy_Extract()
Dim ExtFile As String
ExtFile = Application.GetOpenFilename
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set wbRpt = ThisWorkbook
Set wbExt = Application.Workbooks.Open(ExtFile, _
UpdateLinks:=False, _
ReadOnly:=True, _
addtomru:=False)
wbExt.Sheets(1).Copy After:=wbRpt.Sheets(1)
wbRpt.Sheets(2).Name = "Extracts"
wbExt.Close False
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub