Insert a blank row in between 2 values

ceclay

Board Regular
Joined
Dec 4, 2019
Messages
58
Office Version
  1. 2016
Platform
  1. Windows
I have a random list on column "A". What I would like to do is to insert a row if value below "Bills Payment" is "Payment code". Result sample is in Column "C" ut I would like it in Column A.

Book2
ABC
1Bills Payment:Bills Payment:
2Payment Code: 748494
3ButterPayment Code: 748494
4GardeniaButter
5600Gardenia
6Bread600
7ButterBread
8CookiesButter
9Bills Payment:Cookies
10Payment Code: 89721Bills Payment:
11700
12500Payment Code: 89721
13Tasty700
14Gardenia500
15ButterTasty
16Bills Payment:Gardenia
17Lay info: 124364Butter
18Payment Code: 89721Bills Payment:
19700Lay info: 124364
20800Payment Code: 89721
21Absolute700
22Enfamil800
23Absolute
24Enfamil
Sheet1


VBA Code:
Sub Click()



Dim Sh As Worksheet

Dim currentValue As String

Dim previousValue As String





Set Sh = Sheets("Sheet2")



With Sh

'For each cell in the column

For i = 1 To .Cells(.Rows.Count, A).End(xlUp).Row

currentValue = "Bills Payment:"

previousValue = .Range(yourColumn & i).Offset(2, 0).Value



If previousValue = "Payment code:*" Then

.Rows(i).EntireRow.Insert Shift:=xlDown

End If

Next i

End With



End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
See if this does what you want. Test with a copy of your data.
Code assumes the relevant sheet is the active sheet.

VBA Code:
Sub InsertRows()
  Dim i As Long
 
  Application.ScreenUpdating = False
  For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Left(Range("A" & i).Value, 13) = "Payment Code:" And Range("A" & i - 1).Value = "Bills Payment:" Then Rows(i).Insert
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Suppose insertings rows meaning after each row with "Bills Payment:".
VBA Code:
Sub Click()

    Dim Sh As Worksheet

    Application.ScreenUpdating = False
    Set Sh = Sheets("Sheet2")
    With Sh
        For Each vc In .Range("A1", Cells(Rows.Count, "A").End(xlUp))
            If vc.Value = "Bills Payment:" Then
                .Rows(vc.Row + 1).EntireRow.Insert
            End If
        Next vc
    End With
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Upvote 0
Sorry, my mistake. I didn't read post very well.
 
Upvote 0
If you have a lot of data in it really is all just in Column A and formatting is not an issue then this might be an option for you.

VBA Code:
Sub testInsertArray()

    Dim sht As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim arrOut() As Variant
    Dim PymtCnt As Long, i As Long, cntRow As Long
    
    Set sht = Worksheets("Sheet1")
    Set rng = sht.Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
    arr = rng.Value2
    PymtCnt = Application.CountIfs(rng, "Payment Code*")
    
    If PymtCnt = 0 Then Exit Sub
    ReDim arrOut(1 To UBound(arr) + PymtCnt, 1 To 1)
    cntRow = 0
    
    For i = 1 To UBound(arr)
        cntRow = cntRow + 1
        If i <> 1 Then
            If Left(arr(i - 1, 1), 14) = "Bills Payment:" And Left(arr(i, 1), 13) = "Payment Code:" Then
                cntRow = cntRow + 1             ' Skip 1 row
                arrOut(cntRow, 1) = arr(i, 1)
            Else
                arrOut(cntRow, 1) = arr(i, 1)
            End If
        Else
            arrOut(cntRow, 1) = arr(i, 1)
        End If
    Next i
    
    rng.Resize(cntRow).Value = arrOut

End Sub
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,217,028
Messages
6,134,093
Members
449,860
Latest member
SimoD

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