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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top