Inserting a missing row of a sequence, and accomodating a duplicate row

CellA1234

New Member
Joined
Nov 30, 2021
Messages
5
Office Version
  1. 2007
Platform
  1. Windows
A member named Jim May offered some great code to add missing rows in a sequence.
However, the code gets lost if there is a duplicate row.
Would anyone know the code modification. Example:


row 1 John A
row 2 Jane b+

Jim's original code:
*******************************************
Sub Foo()
Dim FNumb As Long, TNumb As Long
Lr = Range("A" & Rows.Count).End(xlUp).Row
TNumb = Range("A" & Lr).Value
Range("A1").Select
FNumb = Selection.Value
Do Until ActiveCell.Value = TNumb
If ActiveCell.Offset(1) = (FNumb + 1) Then
Else
ActiveCell.Offset(1).EntireRow.Insert
End If
FNumb = FNumb + 1
ActiveCell.Offset(1).Select
Loop
End Sub
*******************************************
 

Attachments

  • Example Sequence.jpg
    Example Sequence.jpg
    65.8 KB · Views: 17

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
PlacePersonDayPlacePersonDay
1​
AndyMonday1AndyMonday
3​
GregTuesday2
4​
AnneMondayMisssing rows are inserted3GregTuesday
5​
BradThursdayAnd duplicate rows are accepted4AnneMonday
7​
SheilaFridayin the original sequence5BradThursday
7​
JaneTuesday6
10​
MarkFriday7SheilaFriday
7JaneTuesday
8
9
10MarkFriday
 
Upvote 0
Try this with a copy of your workbook.

VBA Code:
Sub Insert_Rows()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, Extras As Long
  
  a = Range("A1", Range("C" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To Rows.Count, 1 To 3)
  For i = 2 To UBound(a)
    If a(i, 1) = a(i - 1, 1) Then Extras = Extras + 1
    k = a(i, 1) + Extras
    b(k, 1) = a(i, 1)
    b(k, 2) = a(i, 2)
    b(k, 3) = a(i, 3)
  Next i
  With Range("E2:G2").Resize(k)
    .Value = b
    With .Columns(1)
      On Error Resume Next
      .SpecialCells(xlBlanks).FormulaR1C1 = "=1+R[-1]C"
      On Error GoTo 0
      .Value = .Value
    End With
    .Rows(0).Value = Array("Person", "Place", "Day")
  End With
End Sub

My sample data in columns A:C and code result in E:G.

CellA1234.xlsm
ABCDEFG
1PlacePersonDayPersonPlaceDay
21AndyMonday1AndyMonday
33GregTuesday2
44AnneMonday3GregTuesday
55BradThursday4AnneMonday
67SheilaFriday5BradThursday
77JaneTuesday6
810MarkFriday7SheilaFriday
97JaneTuesday
108
119
1210MarkFriday
Sheet1
 
Upvote 0
Solution
That is brilliand
Try this with a copy of your workbook.

VBA Code:
Sub Insert_Rows()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, Extras As Long
 
  a = Range("A1", Range("C" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To Rows.Count, 1 To 3)
  For i = 2 To UBound(a)
    If a(i, 1) = a(i - 1, 1) Then Extras = Extras + 1
    k = a(i, 1) + Extras
    b(k, 1) = a(i, 1)
    b(k, 2) = a(i, 2)
    b(k, 3) = a(i, 3)
  Next i
  With Range("E2:G2").Resize(k)
    .Value = b
    With .Columns(1)
      On Error Resume Next
      .SpecialCells(xlBlanks).FormulaR1C1 = "=1+R[-1]C"
      On Error GoTo 0
      .Value = .Value
    End With
    .Rows(0).Value = Array("Person", "Place", "Day")
  End With
End Sub

My sample data in columns A:C and code result in E:G.

CellA1234.xlsm
ABCDEFG
1PlacePersonDayPersonPlaceDay
21AndyMonday1AndyMonday
33GregTuesday2
44AnneMonday3GregTuesday
55BradThursday4AnneMonday
67SheilaFriday5BradThursday
77JaneTuesday6
810MarkFriday7SheilaFriday
97JaneTuesday
108
119
1210MarkFriday
Sheet1
That is brilliant and genius Peter!! Thank you so much.
I managed to tweak it to four columns as well.
 
Last edited by a moderator:
Upvote 0
You're welcome. Thanks for the follow-up. :)

I have removed the last line of your post. See #5 of the Forum Rules for the reason. But thanks for the thought. ?
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

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