Solved

MODIFY COPY/PASTE code using multiple sheets

Posted on 2006-11-09
6
128 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

760 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

25 Experts available now in Live!

Get 1:1 Help Now