Solved

MODIFY COPY/PASTE code using multiple sheets

Posted on 2006-11-09
6
133 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

631 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