Solved

Copying data records by a certain number of time using VBA

Posted on 2014-11-13
6
136 Views
Last Modified: 2014-11-13
Dear Experts:

A copying macro should perform the following tasks:

Run from C2 till the last filled cell in Column C
Take the number of the individual cells in Column C as the number of copies by which the data record is to be copied down (-1)
For example the 'Scissor data' should be listed three times, the Spoon record is to be listed 4 times and the fork record is to be listed twice after running the macro:

Before running macro:

Column A        Column B              Column C
Item_No          Description              Number
17-54               Scissor                            3
17-55               Spoon                            4
17-56               Fork                               2


After running macro:

Item_No      Description      Number
17-54           Scissor                3
17-54           Scissor                3
17-54           Scissor                3
17-55           Spoon                 4
17-55           Spoon                 4
17-55           Spoon                 4
17-55           Spoon                 4
17-56           Fork                     2
17-56           Fork                     2


Help is much appreciated. Thank you very much in advance.

I have attached a sample file for your convenience.

Regards, Andreas

Replicate-Data-Records-using-VBA.xlsm
0
Comment
Question by:AndreasHermle
6 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 165 total points
ID: 40439731
Please find attached.

So you can extend columns A-C, I have put the results into column E-G.
Replicate-Data-Records-using-VBA.xlsm
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 167 total points
ID: 40439737
Try this:
Sub Replicator()
    Dim vData
    Dim LR                    As Long
    Dim x                     As Long
    Dim y                     As Long
    Dim j                     As Long
    Dim counter               As Long
    Dim vOut()

    LR = Cells(Rows.Count, "C").End(xlUp).row

    vData = Range("A2:C" & LR).Value
    ReDim vOut(1 To Application.Sum(Range("C2:C" & LR)), 1 To UBound(vData, 2))
    counter = 1
    For x = LBound(vData, 1) To UBound(vData, 1)
        For j = 1 To vData(x, 3)
            For y = LBound(vData, 2) To UBound(vData, 2)
                vOut(counter, y) = vData(x, y)
            Next y
            counter = counter + 1
        Next j
    Next x
    Range("A2").Resize(UBound(vOut, 1), UBound(vOut, 2)).Value = vOut
End Sub

Open in new window

0
 
LVL 35

Accepted Solution

by:
Kimputer earned 168 total points
ID: 40439750
All different code doing the same thing I see :) All finished around the same time too:

Sub test()

UsedRange = ActiveSheet.UsedRange.Rows.Count

For i = UsedRange To 2 Step -1
    j = ActiveSheet.Cells(i, 3)
    For k = j - 1 To 1 Step -1
        Set Rng = ActiveSheet.Range("C" & i).EntireRow
        Rng.Copy
        Rng.Offset(1).Insert Shift:=xlDown
    Next
Next


End Sub

Open in new window


I just went for simplicity and readability (somewhat).
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:AndreasHermle
ID: 40439767
Whow, I am impressed by the speed you answered. Will do some testing shortly and then let you know.

Thank you so much.

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
ID: 40439947
Great job from all of you. All of them work. I am really glad and you saved my day.
0
 

Author Closing Comment

by:AndreasHermle
ID: 40439957
Thank you very much for your swift and professional help. You saved me lots and lots of time. What a forum!!! ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now