+ Reply to Thread
Results 1 to 5 of 5

Macro to copy and paste changing number of rows

Hybrid View

phil3061 Macro to copy and paste... 04-05-2012, 02:13 PM
DGagnon Re: Macro to copy and paste... 04-05-2012, 02:57 PM
Dennis7849 Re: Macro to copy and paste... 04-05-2012, 02:59 PM
Richard Buttrey Re: Macro to copy and paste... 04-05-2012, 03:22 PM
phil3061 Re: Macro to copy and paste... 04-05-2012, 04:55 PM
  1. #1
    Registered User
    Join Date
    10-12-2011
    Location
    New Castle, PA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Macro to copy and paste changing number of rows

    Hello,

    I'm looking for help for a macro that will copy and paste a range of data onto a new sheet. I've attached the workbook, with some sample data. Basically what I need is for each header, ie "Mega AHT", copy the data below it (A3 - F7) then paste it onto a new sheet. Then come back and grab "Inet AHT" (A9 - F14), copy and paste it onto a new sheet.

    This is a daily report, so the range of the data changes daily. Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Macro to copy and paste changing number of rows

    Try this Macro out

    Sub CopyToNewSheet()
    Dim WS As Worksheet
    
    i = 1
    Start = 1
    With Worksheets("CRC_OpsScorecard_Macro")
        Do Until i > .Range("A1000").End(xlUp).Row
            Set WS = Sheets.Add
            WS.Name = .Range("A" & i).Value
            
            Do Until .Range("A" & i).Value = "Period to Date"
                i = i + 1
            Loop
            
            .Range("A" & Start & ":A" & i).EntireRow.Copy Destination:=WS.Range("A1")
            Start = i + 1
            i = i + 1
        Loop
    End With
    
    End Sub
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: Macro to copy and paste changing number of rows

    How many blocks are there that need to be setup on their own sheets?

    Are the number of rows for each block always the same(like always 7 rows tall)??

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to copy and paste changing number of rows

    Hi,

    One way
    Sub CopyToSheets()
        Dim lFirstRow As Long, llastrow As Long, lEndRow As Long, lRow As Long, stHeader As String
        lEndRow = Range("A" & Rows.Count).End(xlUp).Row
        lRow = 1
        
        Do Until lRow > lEndRow
            If Left(Sheet1.Range("A" & lRow), 4) = "Days" Then
                lFirstRow = lRow - 1
                stHeader = Sheet1.Range("A" & lFirstRow)
            End If
            If Left(Sheet1.Range("A" & lRow), 6) = "Period" Then
                llastrow = lRow
            End If
            
            lRow = lRow + 1
    
            If llastrow > lFirstRow Then
                Sheet1.Range("A" & lFirstRow & ":A" & llastrow).EntireRow.Copy
                Sheets.Add after:=Sheets(Sheets.Count)
                ActiveSheet.Paste
                ActiveSheet.Name = stHeader
                lFirstRow = llastrow
            End If
        Loop
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Registered User
    Join Date
    10-12-2011
    Location
    New Castle, PA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro to copy and paste changing number of rows

    You guys are awesome. They both worked great. Thanks so much!!

+ 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