Macro to copy range from one workbook to another

shantanu

New Member
Joined
Jul 30, 2010
Messages
22
Hello,

I am trying to copy a specific data range from all 7 worksheets in one workbook (Sourcebook)to a single summary page of another workbook (Destbook).

The way I do it is as follows:

1) Open the Sourcebook
2) copy the range from Sourcebook.worksheet1
3) Paste the range in Destbook.Sheet1
4) Problem occurs when I try to copy the same range from the sourcebook.worksheet2. It seems that the previous data copied is still selected. I cannot understand where I am going wrong.


Code is presented below:
'to copy data from the workbook for every worksheet'

For TINdataIndex = 1 To 7

SourceBook.Sheets(TINdataIndex).Range(Cells(7, 26), Cells(30, 77)).Select
Selection.Copy

DestBook.Activate
DestCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

ActiveCell.Offset(20, 0).Select

Set DestCell = ActiveCell

SourceBook.Activate
Application.ScreenUpdating = True

Next TINdataIndex
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
Not tested, but presuming I'm reading your code correctly, you should get an error, the descript of which is akin to "Select method ... failed..." in your second time thru the loop.

The problem is that (a bit of guessing) the first sheet is most likely active at the start. Hence, the first time thru the loop, we select a range w/no problem. The second time around though, you are trying to select a range on the second sheet, which has not been activated yet.

Select/Selection/Activate/Active are rarely necessary, but to do it the way you are trying, try:

Rich (BB code):
    For TINdataIndex = 1 To 7
    
        SourceBook.Worksheets(TINdataIndex).Select
        SourceBook.Range(Cells(7, 26), Cells(30, 77)).Select
        Selection.Copy
        
        DestBook.Activate
        
        DestCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                              SkipBlanks:=False, Transpose:=False
        
        ActiveCell.Offset(20, 0).Select
        
        Set DestCell = ActiveCell
        
        SourceBook.Activate
        Application.ScreenUpdating = True
    
    Next TINdataIndex
End Sub
Again, not tested, so please try in a junk copy of your wb.

Hope that helps,

Mark
 

shantanu

New Member
Joined
Jul 30, 2010
Messages
22
Thanks for the hint GTO. The following code worked.


Code:
For TINdataIndex = 1 To 7
    
    SourceBook.Worksheets(TINdataIndex).Activate
    SourceBook.Worksheets(TINdataIndex).Range(Cells(7, 26), Cells(30, 77)).Select
    Selection.Copy
         
    DestBook.Activate
    DestCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    ActiveCell.Offset(20, 0).Select
    
    Set DestCell = ActiveCell
    
    SourceBook.Activate
    Application.ScreenUpdating = True
    
     Next TINdataIndex
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,781
Messages
5,513,408
Members
408,950
Latest member
bndouglas

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top