Create a vba code to insert 2 rows based on one-column condition

csimarketing

New Member
Joined
Jun 18, 2019
Messages
15
Office Version
  1. 2019
Platform
  1. MacOS
I posted a question in a similar thread (Other Thread), but got a message that I should post a new thread. I based my VBA code on the previous thread mentioned but changed it to only a one-column condition. Now I need it to insert 2 rows instead of just one. I feel like I am missing something really simple here, but I played around with the code a bit and cannot get it quite right... any help would be great!

VBA Code:
Option Explicit
Sub InsertRowsWhenValueInColumnAChanges()

    Dim wsSrc As Worksheet
    Dim lngLastRow, lngRow As Long
    Dim strKey As String
  
    Application.ScreenUpdating = False
  
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<- Sheet name containing the data. Change to suit if necessary.
    lngLastRow = wsSrc.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  
    For lngRow = lngLastRow To 2 Step -1
        If lngRow > 2 Then
            If Len(strKey) = 0 Then
                strKey = wsSrc.Range("A" & lngRow)
            End If
            If wsSrc.Range("A" & lngRow - 1) <> strKey Then
                Rows(lngRow).Insert
                strKey = wsSrc.Range("A" & lngRow - 1)
            End If
        End If
    Next lngRow
  
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi csimarketing,

Try this:

VBA Code:
Option Explicit
Sub InsertRowsWhenValueInColumnAChanges()

    Dim wsSrc As Worksheet
    Dim lngLastRow, lngRow As Long, i As Long
    Dim strSrc As String, strKey As String
 
    'Application.ScreenUpdating = False
 
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<- Sheet name containing the data. Change to suit if necessary.
    strSrc = "A" '<- Column containing data. Change to suit if necessary.
    lngLastRow = wsSrc.Range(strSrc & ":" & strSrc).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
    For lngRow = lngLastRow To 2 Step -1
        If lngRow > 2 Then
            If Len(strKey) = 0 Then
                strKey = wsSrc.Range(strSrc & lngRow)
            End If
            If wsSrc.Range(strSrc & lngRow - 1) <> strKey Then
                wsSrc.Range(strSrc & lngRow).Resize(2).EntireRow.Insert
                strKey = wsSrc.Range(strSrc & lngRow - 1)
            End If
        End If
    Next lngRow
 
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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