VBA & Macro duplicating rows

HarryHarry

New Member
Joined
Sep 30, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello guys,

I'm new to macros and I need something for work which has been doing my head in, I can't figure it out.

I have a sheet with 1500 entries/rows. The column S contains frequency for each entry (monthly, quarterly, weekly...).

I need a macro that will check the column S and if it sees 'Monthly' then it will duplicate the row 11 times to get 12 in total, if quarterly then duplicate it 3 times to get 4 and so on for each row.

Furthermore I need to add a column next to the column S to indicate the number of the month per each row as well

Would anyone be kind enough to help me before I go insane?

Thanks a lot!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi Harry,

Try below code ...
VBA Code:
Sub test()

Dim Lc&, r&

With ActiveSheet.[A1].CurrentRegion
   Lc = .Columns.Count + 1
   For x = .Rows.Count To 1 Step -1
      Select Case LCase(.Cells(x, "S"))
         Case "monthly": r = 11
         Case "quarterly": r = 3
      End Select
      If r > 0 Then
         .Rows(x).Offset(1).Resize(r, Lc).Insert
         .Rows(x).Offset(1).Resize(r, Lc) = .Rows(x).Value
         .Parent.Cells(x, Lc).Resize(r + 1, 1) = Evaluate("row(1:" & r + 1 & ")")
         r = 0
      End If
   Next
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,046
Members
449,482
Latest member
al mugheen

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