Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

macro 1

Status
Not open for further replies.

goofy

Programmer
Feb 3, 2001
30
US
i need to copy sheets from different excel workbooks to a single workbooks ,is it possible to write a macro.
 
Yes!
You can get the copy function by recording a macro. Then, just incorporate the opening/closing of different workbooks.
Code:
Sheets("SomeSheet").Select
Sheets("SomeSheet").Copy After:=Workbooks("Book2").Sheets(1)
Do you have a set number of workbooks/sheets that you want to copy, or are you going to have to pull them from a specific directory? If you provide more details, I can provide a more detailed solution.
 
Wht if the sheets are present in differnt path...... will I be able to write a macro to open the excel workbook from diffrent location and copy their worksheets to a new workbook.Plz reply asap.
 
Here is a sample that copies a sheet from 2 books into a master workbook. This code was written to be included in the master (destination) workbook.
Code:
Public Sub CopySheets()
    Dim wbMaster As Workbook
    Dim wbTmp As Workbook
    
    'wbMaster is this workbook - (destination)
    Set wbMaster = ThisWorkbook
    
    'Copy DataSheet from File1
    Workbooks.Open "C:\File1.xls"
    Set wbTmp = ActiveWorkbook
    wbTmp.Sheets("DataSheet").Select
    wbTmp.Sheets("DataSheet").Copy After:=wbMaster.Sheets(1)
    wbTmp.Close SaveChanges:=False

    'Copy DataSheet from File2
    Workbooks.Open "C:\File2.xls"
    Set wbTmp = ActiveWorkbook
    wbTmp.Sheets("SomeSheet").Select
    wbTmp.Sheets("SomeSheet").Copy After:=wbMaster.Sheets(1)
    wbTmp.Close SaveChanges:=False
    
    Set wbTmp = Nothing
    Set wbMaster = Nothing
End Sub
NOTE: If you are copying sheets with like names, you may want to add some type of renaming code.

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top