vba to duplicate blocks of text

kitkat22

New Member
Joined
May 8, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet with many blocks of rows that I need to duplicate. In column I, some cells have the number 7. I want to duplicate the entire block of rows that include the row with the 7 and all the rows with no 7 underneath it, until you get to the next row with a 7 in column I. Then I want to repeat the process for that block of rows, all the way down to the end of the sheet. If the duplicated rows could be a new color, that would be even better. I hope this is clear!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to Mr. Excel.

Where do you want the results?

It would be helpful if you provided a sample of data and the results you expect...AND, please use XL2BB to show your sample.
 
Upvote 0
This is the original sheet

Mini-sheet removed by moderator
 
Last edited by a moderator:
Upvote 0
And this is how I want it to look

Mini-sheet removed by moderator
 
Last edited by a moderator:
Upvote 0
Welcome to the MrExcel board!

Give this a try with a copy of your workbook.

VBA Code:
Sub Duplicate_Blocks()
  Dim rA As Range
  Dim cols As Long, rws As Long
  
  Application.ScreenUpdating = False
  cols = ActiveSheet.UsedRange.Columns.Count
  For Each rA In Range("I2:I" & Range("J" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
      If rA.Cells(0).Value = 7 Then
        rws = rA.Rows.Count + 1
        With rA.Rows(0).EntireRow.Resize(rws, cols)
          .Copy
          .Offset(rws).Insert Shift:=xlDown
          .Offset(rws).Interior.Color = RGB(217, 217, 217)
        End With
      End If
  Next rA
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!

Give this a try with a copy of your workbook.

VBA Code:
Sub Duplicate_Blocks()
  Dim rA As Range
  Dim cols As Long, rws As Long
 
  Application.ScreenUpdating = False
  cols = ActiveSheet.UsedRange.Columns.Count
  For Each rA In Range("I2:I" & Range("J" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
      If rA.Cells(0).Value = 7 Then
        rws = rA.Rows.Count + 1
        With rA.Rows(0).EntireRow.Resize(rws, cols)
          .Copy
          .Offset(rws).Insert Shift:=xlDown
          .Offset(rws).Interior.Color = RGB(217, 217, 217)
        End With
      End If
  Next rA
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
This worked perfectly! Thank you so much! Would it be possible to delete my two posts with the spreadsheets and just keep my initial question and solution?
 
Upvote 0
This worked perfectly! Thank you so much!
You're welcome. Glad it worked for you. :)

Would it be possible to delete my two posts with the spreadsheets and just keep my initial question and solution?
On this occasion I have removed the mini-sheets but for the future please ensure that your samples do not include any sensitive data.
 
Upvote 0

Forum statistics

Threads
1,215,781
Messages
6,126,863
Members
449,345
Latest member
CharlieDP

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