VBA Code / Duplicating Records

cidfidou

Board Regular
Joined
Jan 19, 2009
Messages
163
Hey Excel Gurus,

I have found online a really good piece of vba code but unfortunelaty I can not update it myself as I am a new to VBA.

The below code duplicate record based on a constant in cell d (ie qty 5) and add an incremental number to the new duplicated records

The issue is that when the output of the macro reaches around 15000 lines it stops working.


Any help on making sure that the macro wont stop and will deliver the desire result even if the total of new records is >than 15k???


Thanks in advance
Initial data


D990304101USchicagoqty5
D990304109EUParisqty6

<tbody>
</tbody><colgroup><col><col span="3"></colgroup>

After Macro

D990304101USchicago001
D990304101USchicago002
D990304101USchicago003
D990304101USchicago004
D990304101USchicago005
D990304109EUParis001
D990304109EUParis002
D990304109EUParis003
D990304109EUParis004
D990304109EUParis005
D990304109EUParis006

<tbody>
</tbody><colgroup><col><col span="2"><col></colgroup>


Public Sub ExpandRecords()
Dim i As Long, _
j As Long, _
LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Columns("D:D").NumberFormat = "@"
For i = LR To 1 Step -1
If CLng(Right(Range("D" & i).Value, Len(Range("D" & i).Value) - 3)) > 1 Then
With Range("A" & i)
.Offset(1, 0).Resize(CLng(Right(Range("D" & i).Value, Len(Range("D" & i).Value) - 3)) - 1, 1).EntireRow.Insert Shift:=xlDown
.Resize(CLng(Right(Range("D" & i).Value, Len(Range("D" & i).Value) - 3)), 1).EntireRow.Value = Range("A" & i).EntireRow.Value
For j = 1 To CLng(Right(Range("D" & i).Value, Len(Range("D" & i).Value) - 3))
Range("D" & i).Offset(j - 1, 0).Value = Application.Text(j, "000")
Next j
End With
Else
Range("D" & i).Value = Application.Text(1, "000")
End If
Next i
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I don't know why your code stops working after 15,000 rows, but here's an alternative you can try. This assumes your data start in A1 and produces an expanded listing starting in F1. You can adjust the code to alter these ranges.
Code:
Sub ExpandIt()
Dim lR As Long, R As Range, vA As Variant, n As Long, nR As Long
Dim i As Long, j As Long, k As Long
lR = Range("A" & Rows.Count).End(xlUp).Row
Set R = Range("A1", "D" & lR)
vA = R.Value
nR = 1
Application.ScreenUpdating = False
For i = LBound(vA, 1) To UBound(vA, 1)
    n = Replace(vA(i, 4), "qty", "")
    For j = 0 To 2
        Cells(nR, "F").Offset(0, j).Resize(n, 1).Value = vA(i, j + 1)
    Next j
    For k = 1 To n
        Cells(nR + k - 1, "I").Value = k
    Next k
    nR = Range("F" & Rows.Count).End(xlUp).Row + 1
Next i
Range("I:I").NumberFormat = "000"
End Sub
 
Upvote 0
Hi Joe

Thank you so much for your reply as it is working perfectly. On top of that your code is much faster than the original one.

I have to say that i am also really impressed and grateful than gifted people like you are ready to help strangers. Thanks again
 
Upvote 0
Hi Joe

Thank you so much for your reply as it is working perfectly. On top of that your code is much faster than the original one.

I have to say that i am also really impressed and grateful than gifted people like you are ready to help strangers. Thanks again

You are welcome. Thanks for the kind reply.
 
Upvote 0
The question has of course been very well answered by JoeMo.

However, just for interest, and noting that "Records" sometimes also have formats that may need to be transferred as well as just values, another approach is:
Code:
Sub more()
Dim r, x&, c&
c = 1
For Each r In Cells(1).CurrentRegion.Rows
    c = c + x
    x = Split(r.Cells(4), "qty")(1) * 1
    r.Copy Cells(c, 6).Resize(x)
    With Cells(c, 9).Resize(x)
        .Resize(1) = 1
        .DataSeries
        .NumberFormat = "00#"
    End With
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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