VBA code to insert rows so that each item has the same number of rows

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to modify some vba code supplied by Alan Sidman, thank you Alan :).

What I would like to end up with is 3 of each item/name in column A. I think my approach is completely wrong, and would dearly love some direction.

VBA Code:
Option Explicit

Sub CountNames()
    Dim i, r As Integer, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    i = 3
Do Until i = Cells(Rows.Count, 1).End(xlUp).Row
      For r = 1 To 3
        If Range("A" & i) = Range("A" & i - 1) Then
            GoTo Skip
        ElseIf Range("A" & i) <> Range("A" & i - 1) And r = 2 Then
            GoTo Skip
        Else:   Range("A" & i).Offset(1, 0).EntireRow.Insert
                Range("A" & i).Offset(1, 0) = Range("A" & i)
                i = i + 1
        End If
Skip:
      Next r
      i = i + 1
Loop
    MsgBox "completed"
    
End Sub

Book1
A
1Name
2Jeff
3Jeff
4Tom
5Tom
6Fred
7Fred
8Fred
9Dee
10Dee
11KT
12KT
Sheet1
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Figured it out. Just needed a cup of coffee. Not the nicest solution but it works.

VBA Code:
Option Explicit
Sub InsertRowsUpToThreeRows()
    Dim rw     As Long
    Dim cnt    As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
            If .Cells(rw, 1) = .Cells(rw - 1, 1) Then   'compare rows
                cnt = cnt + 1                           'and count if equal
                
            ElseIf cnt = 1 And .Cells(rw, 1) <> .Cells(rw - 1, 1) Then .Cells(rw, 1).EntireRow.Insert 'insert row
                .Cells(rw, 1) = .Cells(rw + 1, 1)       'write value
                cnt = 0                                 'reset counter
            
            ElseIf cnt = 0 And .Cells(rw, 1) <> .Cells(rw - 1, 1) Then .Cells(rw, 1).EntireRow.Insert
                .Cells(rw, 1) = .Cells(rw + 1, 1)       'write value
                cnt = cnt + 1                           'count if value written
                rw = rw + 1                             'add to row counter to compensate for the loop count
            
            Else
                If cnt = 2 And .Cells(rw, 1) <> .Cells(rw - 1, 1) Then cnt = 0  'start next block
            
            End If
        Next rw
    End With
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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