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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,215,676
Messages
6,126,156
Members
449,295
Latest member
DSBerry

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