Copy and paste Cells in Column based on Value in Cell

matt121237

New Member
Joined
Jul 26, 2007
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I'm trying to copy items from column A multiple times based on count in B into another column. I've tried using an i variable to loop and finding the next open cell but my jumble code that I've pieced together is worthless. I know that has to be super simple.

I've shown below the example items and counts in Column A and B and what the result would look like in column D. Can anyone point me in the right direction?

TEST COPY PASTE.xlsx
ABCD
1ITEMCOUNTRESULT
2ABC3ABC
3DEF5ABC
4GHI2ABC
5JKL7DEF
6DEF
7DEF
8DEF
9DEF
10GHI
11GHI
12JKL
13JKL
14JKL
15JKL
16JKL
17JKL
18JKL
Sheet1
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Perhaps...
VBA Code:
Option Explicit
Sub matt121237()
    Application.ScreenUpdating = False
    Dim rng As Range, c As Range, lr As Long, qty As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    lr = ws.Cells(Rows.Count, "D").End(xlUp).Row + 1
    Set rng = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
    
    For Each c In rng
        qty = c.Offset(, 1)
        ws.Cells(lr, 4).Resize(qty).Value2 = c
        lr = ws.Cells(Rows.Count, 4).End(3).Row + 1
    Next c
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Perhaps...
VBA Code:
Option Explicit
Sub matt121237()
    Application.ScreenUpdating = False
    Dim rng As Range, c As Range, lr As Long, qty As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    lr = ws.Cells(Rows.Count, "D").End(xlUp).Row + 1
    Set rng = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
   
    For Each c In rng
        qty = c.Offset(, 1)
        ws.Cells(lr, 4).Resize(qty).Value2 = c
        lr = ws.Cells(Rows.Count, 4).End(3).Row + 1
    Next c
   
    Application.ScreenUpdating = True
End Sub
I will try as soon as I get back tonight!!! Thanks so much, I can see right off where my issue was in a few spots!!! Thanks again for the quick response.
 
Upvote 0
Thanks Matt. Slight amendment in case of empty cell:

VBA Code:
For Each c In rng
        qty = c.Offset(, 1)
        If qty > 0 Then ws.Cells(lr, 4).Resize(qty).Value2 = c
        lr = ws.Cells(Rows.Count, 4).End(3).Row + 1
    Next c
 
Upvote 0
@matt121237 - I assume the question has been answered since you marked your post as the solution. However, that would be great if you could mark the post as the solution that answered your question in your future threads to help future readers. No further action is required for this question as I already switched the marked solution.
 
Upvote 0
@matt121237 - I assume the question has been answered since you marked your post as the solution. However, that would be great if you could mark the post as the solution that answered your question in your future threads to help future readers. No further action is required for this question as I already switched the marked solution

I hadn't had the chance to test yet and was waiting to do so before marking it. Appreciate your help though.
Thanks Matt. Slight amendment in case of empty cell:

VBA Code:
For Each c In rng
        qty = c.Offset(, 1)
        If qty > 0 Then ws.Cells(lr, 4).Resize(qty).Value2 = c
        lr = ws.Cells(Rows.Count, 4).End(3).Row + 1
    Next c

Worked Flawlessly. Thanks Kevin.
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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