+ Reply to Thread
Results 1 to 19 of 19

Macro to create multiple sheet, copy certain values and loop until end of row data

Hybrid View

  1. #1
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Thumbs up Macro to create multiple sheet, copy certain values and loop until end of row data

    Hi, guys! I news help on this one. I would like to create a macro to copy sheet, name it based on cells from another sheet. I managed, through research, to execute it through looping. see code below:

    Sub copySheet2()
    Dim rngName As Range
    Dim i As Integer
    Set rngName = ThisWorkbook.Sheets("Sheet1").Range("a1")
    Do Until rngName.Value = ""
    i = ThisWorkbook.Sheets.Count
    Sheets("Sheet2").Copy After:=Sheets(i)
    ThisWorkbook.Sheets(i + 1).Name = rngName.Value
    Set rngName = rngName.Offset(1)
    Loop
    End Sub
    It worked well, however, I also need to link or copy values corresponding to each name before it stops executing e.g. Name, Position from Sheet1. I would appreciate any help. Thanks!
    Last edited by jhoelski; 02-17-2012 at 07:29 AM. Reason: Kudos to Rory!

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro Help

    Please add code tags to your code in accordance with forum rules.
    Good luck.

  3. #3
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Macro to create multiple sheet, copy certain values and loop until end of row data

    Sorry for that I'm just a newbie here. Anyway, here is the code:

    Sub copySheet2()
    Dim rngName As Range
    Dim i As Integer
    Set rngName = ThisWorkbook.Sheets("Sheet1").Range("a1")
    Do Until rngName.Value = ""
    i = ThisWorkbook.Sheets.Count
    Sheets("Sheet2").Copy After:=Sheets(i)
    ThisWorkbook.Sheets(i + 1).Name = rngName.Value
    Set rngName = rngName.Offset(1)
    Loop
    End Sub
    Last edited by jhoelski; 02-16-2012 at 08:35 AM. Reason: Abidance with forum rules

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Quote Originally Posted by jhoelski View Post
    I also need to link or copy values corresponding to each name before it stops executing e.g. Name, Position from Sheet1. I would apprecGhiate any help.
    Could you clarify what you mean by this?

  5. #5
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Well, i have two sheets, sheet1 contains the names, sheet2 is the one that will be created by the macro many times and then rename it based on sheet1 row data starting from A1 until blank, hence the loop. However, I also need to copy values on sheet1, particularly column b1 & c1 to each worksheet. My data is like these:


    Sheet1
    A. B. C.
    1 Name1 rank office
    2 Name 2 rank office
    3 Name 3 rank office

    sheet2 (the one to be copied)
    Has cells for name, rank & office

    When the macro runs, it creates worksheets for each name listed. What i need is to add a block of code that will also copy the name, rank & office. thanks!

  6. #6
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    I think you need something like this:
    Sub copySheet2()
    Dim rngName As Range
    Dim i As Integer
    Set rngName = ThisWorkbook.Sheets("Sheet1").Range("a1")
    Do Until rngName.Value = ""
    i = ThisWorkbook.Sheets.Count
    Sheets("Sheet2").Copy After:=Sheets(i)
    With ThisWorkbook.Sheets(i + 1)
       .Name = rngName.Value
       ' copy name and two adjacent cells to A1 on new sheet
       .Range("A1").resize(, 3).value = rngName.resize(, 3).value
    End With
    Set rngName = rngName.Offset(1)
    Loop
    End Sub

  7. #7
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

       ' copy name and two adjacent cells to A1 on new sheet
       .Range("A1").resize(, 3).value = rngName.resize(, 3).value
    Now running but output is not as desired, I appreciate it though. Rory, can you tell me how this code works?! I mean, can you elaborate what it is supposed to do? Enlighten me on this one, perhaps this would do the trick. Thanks!

  8. #8
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    I tested the code, it doesn't seem to be working. No error, just not executing as it is supposed to. Help! Thanks!

  9. #9
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    I need more to go on than that.

  10. #10
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    It should populate range A1:C1 on the new sheet with the name of the sheet (from Sheet1) and the two cells to the right of the name.

  11. #11
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Okay. What if I need to select the following range C:14 to E14?! They are three cells, adjacent cells I need to auto-populate, to the right of B14 from which the name of the worksheet, created by macro, is derived. Thanks again, Rory.

  12. #12
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Assuming you still want to populate A1:C1 on each sheet
    .Range("A1").resize(, 3).value = rngName.Offset(0, 1).resize(, 3).value

  13. #13
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Got it,Rory! You've been a big help! One more request, what if aside from the autopopulate there is a bunch formula as well which are linked to the autopopulated sheets made by the macro, they are on absolute reference. Let me make it clear:

    A. B. C. D. E.
    1 name rank office =sheet2!b$4$ =sheet2!b$6$
    2 ..
    3 ..

    The macro you gave me really saved me. I would appreciate it if you could help me further. Thanks again, Rory.
    Last edited by jhoelski; 02-16-2012 at 11:43 PM.

  14. #14
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Let me summarize what the macro intends to do:
    1. Create copy of sheet2 based on names on sheet1
    2. Rename the sheets accordingly
    3. Auto-populate certain values (name, etc.)
    4. Create formula or absolute references to each named wsheet (on sheet1, adjacent to the name) before it proceeds with creating another worsheet with next name (loop until EOF)

    thanks for all the help!
    Last edited by jhoelski; 02-17-2012 at 12:00 AM.

  15. #15
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Try this:
    Sub copySheet2()
        Dim rngName As Range
        Dim i As Integer
        Set rngName = ThisWorkbook.Sheets("Sheet1").Range("a1")
        Do Until rngName.Value = ""
            i = ThisWorkbook.Sheets.Count
            Sheets("Sheet2").Copy After:=Sheets(i)
            With ThisWorkbook.Sheets(i + 1)
                .Name = rngName.Value
                ' copy name and two adjacent cells to A1 on new sheet
                .Range("A1").Resize(, 3).Value = rngName.Offset(0, 1).Resize(, 3).Value
                ' adjust formulas on sheet1
                rngName.Offset(0, 3).Resize(, 2).Replace what:="Sheet2", replacement:=.Name, _
                                                            lookat:=xlPart, MatchCase:=False
            End With
            Set rngName = rngName.Offset(1)
        Loop
    End Sub

  16. #16
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Hi, Rory. I appreciate the help you have extended so far, kindly refer to the attachment. I've included the sample worksheet I am working on hoping you could decipher what I need to do, thanks again!
    Attached Files Attached Files

  17. #17
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Would have saved time to have that to start with...

    See attached.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    Wow, that was fast! Bravo to you! Tested it and worked! Thanks a bunch, Rory!

  19. #19
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Macro to create multiple sheet, copy certain values and loop until end of row dat

    One more request, Rory, pleassseeee..... Could you auto-populate the formula column as well?! I mean automate it instead of manually typing in the INDEX, MATCH formula for each individual name? Thanks, man!

+ 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