Step wise approach for copying excel data from one excel file to another(when the destination workbook is already created):-
- Create the excel object
- Open the source workbook(excel whose data need to be copied)
- Open the source worksheet(sheet whose data need to be copied)
- Now,open the excel workbook in which we need to copy the data(say destination workbook)
- Open the destination worksheet
- Copy the data from source workbook and paste into destination worksheet.
- Save the destination workbook.
- Set excel object to nothing to free the allocated memory to object.
Function CopyData(SourceExcel,DestnExcel,SourceSheetName,DestnSheetName)
Set ExcelObj=CreateObject("Excel.Application")
Set SourceWorkBook=ExcelObj.WorkBooks.Open(SourceExcel)
Set SourceWorkSheet=SourceWorkBook.WorkSheets(SourceSheetName)
Set DestinationWorkBook=ExcelObj.WorkBooks.Open(DestinationExcel)
Set DestinationWorkSheet=SourceWorkBook.WorkSheets(DestinationSheetName)
SourceWorkSheet.UsedRange.Copy
DestinationWorkSheet.Range("").PasteSpecial Paste=xlValues
DestinationWorkBook.Save
DestinationWorkBook.Close
SourceWorkBook.Close
Set ExcelObj=Nothing
End Function
In the function ,I have assumed that the destination excel file is already created.
No comments:
Post a Comment