Solved

MODIFY COPY/PASTE code using multiple sheets

Posted on 2006-11-09
6
132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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