+ Reply to Thread
Results 1 to 16 of 16

duplicate rows based on cell value

Hybrid View

epi duplicate rows based on cell... 10-28-2010, 11:49 AM
StephenR Re: duplicate rows based on... 10-28-2010, 11:59 AM
epi Re: duplicate rows based on... 10-28-2010, 12:08 PM
epi Re: duplicate rows based on... 10-28-2010, 12:11 PM
StephenR Re: duplicate rows based on... 10-28-2010, 12:15 PM
epi Re: duplicate rows based on... 10-28-2010, 12:20 PM
StephenR Re: duplicate rows based on... 10-28-2010, 12:25 PM
epi Re: duplicate rows based on... 10-28-2010, 12:27 PM
StephenR Re: duplicate rows based on... 10-28-2010, 12:27 PM
epi Re: duplicate rows based on... 10-28-2010, 12:35 PM
StephenR Re: duplicate rows based on... 10-28-2010, 12:37 PM
epi Re: duplicate rows based on... 10-28-2010, 12:39 PM
epi Re: duplicate rows based on... 10-28-2010, 12:51 PM
StephenR Re: duplicate rows based on... 10-28-2010, 02:14 PM
epi Re: duplicate rows based on... 11-01-2010, 09:33 AM
Emilyti Re: duplicate rows based on... 08-05-2014, 11:33 AM
  1. #1
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    duplicate rows based on cell value

    Hey folks,
    I have a dataset that consists of roughly 4000 observations and I need to expand it so that I duplicate rows based on the value noted in the final column.

    Here is a sample of what my data looks like:

    Community Zone Population size Date Event Count
    B SLZ 446 03/27/2006 ILI 5
    B SLZ 446 04/03/2006 ILI 4

    And I'd like for it to look like this:

    Community Zone Population size Date Event Count
    B S 400 03/27/2006 ILI 5
    B S 400 03/27/2006 ILI 5
    B S 400 03/27/2006 ILI 5
    B S 400 03/27/2006 ILI 5
    B S 400 03/27/2006 ILI 5
    B S 400 04/03/2006 ILI 4
    B S 400 04/03/2006 ILI 4
    B S 400 04/03/2006 ILI 4
    B S 400 04/03/2006 ILI 4

    Does anyone know how I can do this using Excel 2003?

    Thanks!

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    Here is one approach, assumes your data in A to F with header in row 1.
    Sub x()
    
    Dim v, i As Long, j As Long
    
    Application.ScreenUpdating = False
    
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 6).Value
    
    ActiveSheet.UsedRange.Offset(1).ClearContents
    
    For i = LBound(v, 1) To UBound(v, 1)
        With Range("A" & Rows.Count).End(xlUp)(2)
            .Resize(v(i, 6)).Value = v(i, 1)
            .Offset(, 1).Resize(v(i, 6)).Value = v(i, 2)
            .Offset(, 2).Resize(v(i, 6)).Value = v(i, 3)
            .Offset(, 3).Resize(v(i, 6)).Value = v(i, 4)
            .Offset(, 4).Resize(v(i, 6)).Value = v(i, 5)
            .Offset(, 5).Resize(v(i, 6)).Value = v(i, 6)
        End With
    Next i
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by StephenR; 10-28-2010 at 12:07 PM.

  3. #3
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    Thanks so much for the reply!
    I'm getting an "application defined/object defined error".

    The following line is highlighted:
    Range("A" & Rows.Count).End(xlUp)(2).Resize(v(i, 6)).Value = v(i, 1)

  4. #4
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    Just a thought but could this be because I have entries where the count is Zero?

    If this is the case, is there any way of keeping/skipping these rows and moving on to cases where the count is not zero?

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    Yes but what do you want to happen to those entries?

  6. #6
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    I'd like to have them still visible within the dataset. I don't want them to be deleted just yet.

    Thanks!

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    OK, try this:
    Sub x()
    
    Dim v, i As Long, j As Long, n As Long
    
    Application.ScreenUpdating = False
    
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 6).Value
    
    ActiveSheet.UsedRange.Offset(1).ClearContents
    
    For i = LBound(v, 1) To UBound(v, 1)
        n = IIf(v(i, 6) = 0, 1, v(i, 6))
        With Range("A" & Rows.Count).End(xlUp)(2)
            .Resize(n).Value = v(i, 1)
            .Offset(, 1).Resize(n).Value = v(i, 2)
            .Offset(, 2).Resize(n).Value = v(i, 3)
            .Offset(, 3).Resize(n).Value = v(i, 4)
            .Offset(, 4).Resize(n).Value = v(i, 5)
            .Offset(, 5).Resize(n).Value = v(i, 6)
        End With
    Next i
    
    Application.ScreenUpdating = True
    
    End Sub

  8. #8
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    It gives an error at the following line:
    n = IIf(v(i, 6) = 0, 1, v(i, 6))

    "Type mismatch error"

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    Can you post a workbook please, and you should use code tags.

  10. #10
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    Hey, Here it is.
    Thanks!
    Attached Files Attached Files

  11. #11
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    Funny, I just ran my code on your file and it worked. Did you try on a different file?

    EDIT: the error suggests a text entry in column F.

  12. #12
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    ya, it was a different file (I sent a smaller version). I also ran the code on this file and if you look at the output, instead of generating 5 entries for the first date, it generated a bunch more.
    Did it work properly for you?

  13. #13
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    I noted a missing entry in the count, and deleted it. Other than this there aren't any non-numerical entries that I could see within the F column. I reran the code again and got a different error:

    Run-time error '1004':
    Application-defined or object-defined error

    When I debug the following line is highlighted in yellow:
    Range("A" & Rows.Count).End(xlUp)(2).Resize(v(i, 6)).Value = v(i, 1)

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: duplicate rows based on cell value

    When I run this code it goes from 208 rows to 944.
    Sub x()
    
    Dim v, i As Long, j As Long, n As Long
    
    Application.ScreenUpdating = False
    
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 6).Value
    
    ActiveSheet.UsedRange.Offset(1).ClearContents
    
    For i = LBound(v, 1) To UBound(v, 1)
        If v(i, 6) = 0 Or Not IsNumeric(v(i, 6)) Then
            n = 1
        Else
            n = v(i, 6)
        End If
        With Range("A" & Rows.Count).End(xlUp)(2)
            .Resize(n).Value = v(i, 1)
            .Offset(, 1).Resize(n).Value = v(i, 2)
            .Offset(, 2).Resize(n).Value = v(i, 3)
            .Offset(, 3).Resize(n).Value = v(i, 4)
            .Offset(, 4).Resize(n).Value = v(i, 5)
            .Offset(, 5).Resize(n).Value = v(i, 6)
        End With
    Next i
    
    Application.ScreenUpdating = True
    
    End Sub

  15. #15
    Registered User
    Join Date
    10-25-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: duplicate rows based on cell value

    Thank you so much Stephen! It works now
    I really appreciate all your help!

  16. #16
    Registered User
    Join Date
    08-05-2014
    Location
    Devon
    MS-Off Ver
    2010
    Posts
    1

    Re: duplicate rows based on cell value

    Just wanted to say that this has solved a problem I've been mulling over for a while. Thanks very 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