Solved

MODIFY COPY/PASTE code using multiple sheets

Posted on 2006-11-09
6
130 Views
Last Modified: 2010-07-27
I need a code to copy data from multiple worksheets (Workbook1, all of the ranges are the same on each sheet =B3:X53).  Paste data into another Workbook2 (worksheet is the same="hour_data" but the ranges are not the same).  I use the following formula to copy and paste data from single worksheets.  Please explain how to maybe use Loop to prevent from copying (for each sheet) this formula 25times.
**There are a few sheets in Workbook1 that I do not want to copy.  All of the worksheets in Workbook1 are labeled (sheet.name).


Workbooks.Open Filename:=("c:\templates\Workbook1.xls")
Sheets("7").Select
    Range("B3:X53").Select
    Selection.Copy
    Windows("Workbook2.xls").Activate
    Sheets("hour_data").Select
    Range("C3:Y53").Select
    ActiveSheet.Paste
   
0
Comment
Question by:smcfarla
  • 4
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17910873
How close is this?

Sub CopyAndPaste()
    Dim wbk1 As Workbook
    Dim wbk2 As Workbook
    Dim i As Integer
    Dim strDestination As String
    Set wbk1 = Workbooks.Open("c:\templates\Workbook1.xls")
    Set wbk2 = Workbooks("Workbook2.xls")
    For i = 1 To 25
        wbk1.Sheets(i + 6).Range("B3:X53").Copy
        strDestination = "C" & 3 + 50 * (i - 1) & ":Y" & 53 + 50 * (i - 1)
        wbk2.Sheets("hour_data").Range(strDestination).Paste
    Next i
End Sub
0
 

Author Comment

by:smcfarla
ID: 17911821
I get an error msg "object doesn't support this object".  When I debug the script this part of the code is highlighted:

"wbk2.Sheets("Sheet1").Range(strDestination).Paste"

How can I get this code to copy in a specific range?

Copy From Workbook1.Sheet1.range (B3:X53)
Copy To Workbook2."hour data".range (c3:Y53)

Copy From Workbook1.Sheet2.range (B3:X53)
Copy To Workbook2."hour data".range (C57:Y107)

Copy From Workbook1.Sheet3.range (B3:X53)
Copy To Workbook2."hour data".range (C111:Y161)

Copy From Workbook1.Sheet4.range (B3:X53)
Copy To Workbook2."hour data".range (C164:Y214)


and so on....

**There are a few sheets in Workbook1 that I do not want to copy.  
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17913258
Sorry. I wrote the code for a test system, and then quickly (too quickly) adapted it for yours.

I've changed the destination location to be more numerical, and offset each row by 54.

Sub CopyAndPaste()
    Dim wbk1 As Workbook
    Dim wbk2 As Workbook
    Dim i As Integer
    Dim r1 As Integer
    Dim r2 As Integer
    Dim c1 As Integer
    Dim c2 As Integer
   
    c1 = 3 'C
    c2 = 25 'Y
    r1 = 3
    r2 = 53
    Set wbk1 = Workbooks.Open("c:\templates\Workbook1.xls")
    Set wbk2 = Workbooks("Workbook2.xls")
    For i = 1 To 25
        Select Case i
            Case 3, 5, 9 'the ones that you don't want to copy
            Case Else
                wbk1.Sheets(i).Range("B3:X53").Copy
                With wbk2.Sheets("hour_data")
                    .Paste .Range(.Cells(r1 + 54 * (i - 1), c1), .Cells(r2 + 54 * (i - 1), c2))
                End With
        End Select
    Next i
End Sub
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:smcfarla
ID: 17914534
The pasted data is blank.  Probably because the copied data in Workbook1 contains formulas, is there a way to paste the vaues?

And is there a way to skip the sheets that i do not want to copy?  If not I'll just place them at the end of the workbook.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17914882
How would the program know which ones not to copy?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17915059
We can use Pastespecial with the first paramter set to xlPasteValues to drop the formulae.

Sub CopyAndPaste()
    Dim wbk1 As Workbook
    Dim wbk2 As Workbook
    Dim i As Integer
    Dim r1 As Integer
    Dim r2 As Integer
    Dim c1 As Integer
    Dim c2 As Integer
   
    c1 = 3 'C
    c2 = 25 'Y
    r1 = 3
    r2 = 53
    Set wbk1 = Workbooks.Open("c:\templates\Workbook1.xls")
    Set wbk2 = Workbooks("Workbook2.xls")
    For i = 1 To 3
        Select Case i
            Case 3, 5, 9 'the ones that you don't want to copy
            Case Else
                wbk1.Sheets(i).Range("B3:X53").Copy
                With wbk2.Sheets("hour_data")
                    .Range(.Cells(r1 + 54 * (i - 1), c1), .Cells(r2 + 54 * (i - 1), c2)).PasteSpecial xlPasteValues
                End With
        End Select
    Next i
End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question