Solved

MODIFY COPY/PASTE code using multiple sheets

Posted on 2006-11-09
6
129 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now