Tuesday, 9 April 2013

Copy Data From One Workbook To Another Workbook

Copy Data From One Workbook To Another Workbook

For This ,both the workbooks is in the same directory or  it can be  in other directory ,
Let us-  Source excel sheet name is "Example.xlsx" and sheet name is  "OscarTorres" .
then firstly we have to find out the location(path) of this workbook. and similarly doing this with another one, where we want  to copy the data. Below I write the simple example  for this, here  is the excel sheet is in the same directory.

Requirement:-
1. We need two worksheet(it may be perform with multiple worksheet).
2. Define the rang.
3. Find out the path of workbook location.

We Can Write this code with module and assign to Form Control Button,and  another way is write in ActiveX Controls Command Button.
Go through this-  Excel Sheet --->Developer Tab--->Insert---> Form Controls---> Button

Here I create one 'Micro' ,and it can be assign to button or run indusualy.

Sub CopyData()
Application.DisplayFullScreen = False

Dim SourceWorkBookName As String
Dim SourceSheetName As String
Dim SourceWorkBook As Excel.Workbook
Dim SourceSheet As Excel.Worksheet
Dim workingDirectory  As String

SourceWorkBookName = "Example.xlsx"
SourceSheetName = "OscarTorres"
workingDirectory = Application.ActiveWorkbook.Path

Set SourceWorkBook = Workbooks.Open(workingDirectory & "\" & SourceWorkBookName)
'ActiveWindow.Visible = False
Set SourceWorkBook = Application.Workbooks(SourceWorkBookName)
Set SourceSheet = SourceWorkBook.Worksheets(SourceSheetName)
 
ThisWorkbook.Sheets(1).Range("A1", "A43").Value = SourceSheet.Range("AE531", "AE573").Value
SourceWorkBook.Close

Application.DisplayFullScreen = True

End Sub

No comments:

Post a Comment