Duplicating Rows Based On A Cell Value

adamtest1

New Member
Joined
Sep 7, 2011
Messages
1
Hi - Does anyone happen to know of a formula or macro that could bacially duplicate rows based on a cell value? The situation is, I have two columns of data; column A is a name and column B is the number of sweepstakes entries they should receive (1-5). These people will be entered into a sweepstakes and will receive muliple entries based on column B (giving them a better chance to win). When pulling random winners, we usually use the formula =RAND and then sort that list from biggest to smallest, giving the biggest number the prize. Since we have to multiply the people's chances based on column B, is there a better way to do this?

I was thinking about simply duplicating the rows based on coulmn B and then =RAND like we usually do, but I'm not sure how to accomplish this.

As an example, this:

Adam 1
Barry 3
Charles 0
David 2

Would be this:

Adam
Barry
Barry
Barry
David
David

And then we would randomize, picking the winner.

Any thoughts?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the Board!

This macro will copy the data from Sheet1 to Sheet2, repeating the number of times indicated. Note that you will need to indicate the source starting row and destination starting row in the VBA code. Currently, I have it to start looking at data in cell A2 on Sheet1, and it pastes data on Sheet2 starting in cell A1.
Code:
Sub MyCopy()
 
    Dim myStartRow As Long
    Dim myDestRow As Long
    Dim myLastRow As Long
    Dim i As Long
    Dim myLoopCount As Long
 
    Application.ScreenUpdating = False
 
'   Set row to start on sheet 1
    myStartRow = 2
    
'   Set row to start on sheet 2
    myDestRow = 1
    
'   Count total rows on sheet 1
    Sheets("Sheet1").Activate
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
        
'   Loop through all people and place on sheet 2
    For i = myStartRow To myLastRow
        myLoopCount = Sheets("Sheet1").Cells(i, "B")
        If myLoopCount > 0 Then
            For j = 1 To myLoopCount
                Sheets("Sheet2").Cells(myDestRow, "A") = Sheets("Sheet1").Cells(i, "A")
                myDestRow = myDestRow + 1
            Next j
        End If
    Next i
    
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Hi - Does anyone happen to know of a formula or macro that could bacially duplicate rows based on a cell value? The situation is, I have two columns of data; column A is a name and column B is the number of sweepstakes entries they should receive (1-5). These people will be entered into a sweepstakes and will receive muliple entries based on column B (giving them a better chance to win). When pulling random winners, we usually use the formula =RAND and then sort that list from biggest to smallest, giving the biggest number the prize. Since we have to multiply the people's chances based on column B, is there a better way to do this?

I was thinking about simply duplicating the rows based on coulmn B and then =RAND like we usually do, but I'm not sure how to accomplish this.

As an example, this:

Adam 1
Barry 3
Charles 0
David 2

Would be this:

Adam
Barry
Barry
Barry
David
David

And then we would randomize, picking the winner.

Any thoughts?

Maybe:

Code:
Sub adamtest1()
Dim i As Long
Dim lr As Long
Dim x As Integer

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 2 Step -1

x = Range("B" & i).Value

    If Range("B" & i).Value > 1 Then
        
        Range("A" & i).Copy
    
        Range("A" & i).Resize(x).Insert shift:=xlDown
        
        Range("B" & i).Clear
        
    End If
    
    If Range("B" & i).Value < 1 Then Range("A" & i).Delete shift:=xlUp
    
Next i



End Sub
 
Upvote 0
Maybe:

Code:
Sub adamtest1()
Dim i As Long
Dim lr As Long
Dim x As Integer

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 2 Step -1

x = Range("B" & i).Value

    If Range("B" & i).Value > 1 Then
        
        Range("A" & i).Copy
    
        Range("A" & i).Resize(x).Insert shift:=xlDown
        
        Range("B" & i).Clear
        
    End If
    
    If Range("B" & i).Value < 1 Then Range("A" & i).Delete shift:=xlUp
    
Next i



End Sub

Better yet:

Code:
Sub adamtest1()
Dim i As Long
Dim lr As Long
Dim x As Integer

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 2 Step -1

x = Range("B" & i).Value

    If x > 1 Then
        
        Range("A" & i).Copy
    
        Range("A" & i).Resize(x - 1).Insert shift:=xlDown
        
    End If
    
    If x < 1 Then Range("A" & i).Delete shift:=xlUp
    
    
Next i

Range("B2:B" & lr).Clear

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top