Need to copy cells after last used cell in same column based on the value of the cell in the adjacent column on the right

Pandrade

New Member
Joined
Apr 13, 2020
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need to duplicate values in a column based on the value of the cell to the right of it, for example if the Quantity column is 1 then no values are duplicated, if the quantity is 2 then the cell to the left (the salary) is copied and pasted at the end of the salary column once, if the quantity is 3 then the salary is copied at the end of the salary column two times. In other words, if the quantity is 1, then the salary should only appear once in the salary column, if the quantity is 2, then the salary should appear twice in the salary column, if the quantity is 3, then the salary should appear for a total of three times in the salary column. Please help, there has to be a way to write VBA for this.

Salary Quantity
15,000 1
20,000 2
27,000 1
30,000 3
41,000 4

After running the macro the final result should be:
Salary Quantity
15,000 1
20,000 2
27,000 1
30,000 3
41,000 4
20,000
30,000
30,000
41,000
41,000
41,000
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Pandrade()
   Dim Cl As Range
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Cl.Offset(, 1).Value > 1 Then
         Cl.Copy Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1 * Cl.Offset(, 1) - 1)
      End If
   Next Cl
End Sub
 
Upvote 0
Thanks for your reply. I tried the code but nothing happens. Below are the screenshots of what the data is like and what it should look like after the macro.
1586798662294.png
1586798675907.png
 
Upvote 0
That's because you never said where you data was, so I assumed it was in col A, which is pretty much standard practise. ;)
VBA Code:
Sub Pandrade()
   Dim Cl As Range
   For Each Cl In Range("N2", Range("N" & Rows.Count).End(xlUp))
      If Cl.Offset(, 1).Value > 1 Then
         Cl.Copy Range("N" & Rows.Count).End(xlUp).Offset(1).Resize(1 * Cl.Offset(, 1) - 1)
      End If
   Next Cl
End Sub
 
Upvote 0
That's great! It works perfectly! Thank you so much. You made my day.
 
Upvote 0
How can I get that macro to work on every other column in the worksheet, not just on column N?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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