VBA to copy and paste a list over and over

Cr864

New Member
Joined
Dec 30, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi, im looking for some VBA code to be able to take the data in Column A and Paste it in a column, a number of times equal to its value in column B. I need the output to look like the second xl2BB sheet below. It's important that the data goes in order from top to bottom each time it is posted, and that the data in column A only shows up a number of times equal to its value in column B.


Thank you for any help!

Auto200.xlsm
AB
6A1
7B2
8C3
9D4
10E5
11F6
Variables


Auto200.xlsm
D
6A
7B
8C
9D
10E
11F
12B
13C
14D
15E
16F
17C
18D
19E
20F
21D
22E
23F
24E
25F
26F
Variables
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this macro:
VBA Code:
Sub KeepCopying()
Dim LastA As Long, I As Long, Rpt As Long
Dim Dest As String, NextG As Long
'
Dest = "H5"              '<<< The position for the resulting list
'
LastA = Cells(Rows.Count, "A").End(xlUp).Row
Do
    Rpt = 0
    NextG = Range(Dest).Offset(10000, 0).End(xlUp).Row
    If NextG < Range(Dest).Row Then NextG = 0 Else NextG = NextG - Range(Dest).Row + 1
'    Cells(Rows.Count, OCol).End(xlUp).Row 1
    For I = 1 To LastA
        If Application.WorksheetFunction.CountIf(Range(Dest).Resize(1 + NextG + Rpt, 1), Cells(I, "A")) < Cells(I, "B").Value Then
            Range(Dest).Offset(NextG + Rpt, 0).Value = Cells(I, "A")
            Rpt = Rpt + 1
        End If
    Next I
    If Rpt = 0 Then Exit Do
    DoEvents
Loop
End Sub
Adapt the line marked <<< to your preference

Bye
 
Upvote 0
Solution
Try this macro:
VBA Code:
Sub KeepCopying()
Dim LastA As Long, I As Long, Rpt As Long
Dim Dest As String, NextG As Long
'
Dest = "H5"              '<<< The position for the resulting list
'
LastA = Cells(Rows.Count, "A").End(xlUp).Row
Do
    Rpt = 0
    NextG = Range(Dest).Offset(10000, 0).End(xlUp).Row
    If NextG < Range(Dest).Row Then NextG = 0 Else NextG = NextG - Range(Dest).Row + 1
'    Cells(Rows.Count, OCol).End(xlUp).Row 1
    For I = 1 To LastA
        If Application.WorksheetFunction.CountIf(Range(Dest).Resize(1 + NextG + Rpt, 1), Cells(I, "A")) < Cells(I, "B").Value Then
            Range(Dest).Offset(NextG + Rpt, 0).Value = Cells(I, "A")
            Rpt = Rpt + 1
        End If
    Next I
    If Rpt = 0 Then Exit Do
    DoEvents
Loop
End Sub
Adapt the line marked <<< to your preference

Bye
This works great!

Thank you!
 
Upvote 0
thank you for the feedback
Is there an easy way to filter the destination range and only copy these into the visible cells after the filtering is done? I tried turning on an autofilter for the term im looking for, but it was copying into non visible cells as well.

Thanks Anthony!
 
Upvote 0
The copied range can be non-contiguous cells, but the pasted range will be of contiguous cells.

Try explaining further wich is the need and maybe a solution will be found

Bye
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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