+ Reply to Thread
Results 1 to 6 of 6

Copy data from single sheet to multiple sheets

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,173

    Copy data from single sheet to multiple sheets

    I recieve a daily spreadsheet with ~25,000 rows of data and is 4 columns wide.

    Each day I need to break the data equally up into 19 or 20 different sheets within the same workbook. This is very time consuming as I need to copy the first ~1,000 rows and paste it into the first sheet, then I need to take the next ~1,000 rows and paste it into the second sheet, and so on.

    I have attached a small example with desired output. In my example, the raw data file contains 19 records. For these 19 records, I need to distribute the records evenly between 5 individual sheets. In this case, each sheet will contain 4 records or less.
    Last edited by maacmaac; 01-23-2009 at 11:32 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,173
    I almost have it figured out but getting an error on following section of code. I've tried adjusting the code without the quotes, combination of quotes, etc., but nothing seems to work.

    Sub Allocate2()
    
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim i As Integer
    
    FirstRow = 2
    LastRow = 5
    
    Sheets("RawData").Select
    
        For i = 2 To Sheets.Count
            Rows("FirstRow:LastRow").Copy
    
            With Sheets(i)
                .Range("A2").Paste
            End With
            
            FirstRow = FirstRow + 4
            LastRow = LastRow + 4
        Next i
    
    End Sub
    This part of the code works fine if I use
    Rows("2:5").Copy
    I just can't get it to work when using the variable names.

    Any help is appreciated. Thanks.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello maacmaac,

    There are two ways to accomplish this. Use strings are you have in your code, or use the Range method. I show you both...
    Sub Allocate2()
    
    Dim FirstRow As Variant
    Dim LastRow As Variant
    Dim i As Integer
    
    FirstRow = 2
    LastRow = 5
    
    Sheets("RawData").Select
    
        For i = 2 To Sheets.Count
           'String method
            Rows(FirstRow & ":" & LastRow).Copy
    
           'Range Method
            'Range(Rows(FirstRow), Rows(LastRow)).Copy
    
            With Sheets(i)
                .Range("A2").Paste
            End With
            
            FirstRow = FirstRow + 4
            LastRow = LastRow + 4
        Next i
    
    End Sub
    Sincerely,
    Leith Ross

  4. #4
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,173
    Leith,

    Thanks for the comments. I ran into one more issue which is the next part of the code. The code I want to use is as follows but doesn't work.
        For i = 2 To Sheets.Count
            Rows(FirstRow & ":" & LastRow).Copy
    
            With Sheets(i)
                .Range("A2").Paste
            End With
            
            FirstRow = FirstRow + 4
            LastRow = LastRow + 4
        Next i
    The code will work if the code is like this
        For i = 2 To Sheets.Count
            Rows(FirstRow & ":" & LastRow).Copy
    
            Sheets(i).Select
            Range("A2").Select
            ActiveSheet.Paste
            Sheets("RawData").Select
            
            FirstRow = FirstRow + 61
            LastRow = LastRow + 61
        Next i
    Thanks

    P.S. is the first example the more effiecient way write the code?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello maacmaac,

    Sorry, I was in a hurry and mistyped that line. Change it to this...
         For i = 2 To Sheets.Count
            Rows(FirstRow & ":" & LastRow).Copy
               Sheets(i).Range("A2").PasteSpecial Paste:=xlPasteAll
            FirstRow = FirstRow + 4
            LastRow = LastRow + 4
        Next i
    Sincerely,
    Leith Ross

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,173
    Working perfect. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1