VBA - insert a blank row over each new unique value in column S

apwr

New Member
Joined
Apr 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I want to insert a new line over each unique value in column S. The value in column S needs to be copied to column I. I also want to copy values from column A and B to the new line. See the example below:

example_excel.jpg


It is important to keep the lines that not have a value in column S.

Thanks for your response.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
@apwr Welcome to MrExcel.

Test this code and see if it helps.

VBA Code:
Sub Add_Rows()
Dim r  As Long
Dim ur As Long
Dim srng As Range
Dim val As Variant

'last row of datain s
r = Range("S" & Rows.Count).End(xlUp).Row
'populated range in S
Set srng = Range("S1:S" & r)

Application.ScreenUpdating = False
Do While r > 1
val = Range("S" & r).Value
    If Not val = xlnullstring Then
    'get row of first instance of value in S
    mr = WorksheetFunction.Match(val, srng, 0)
    ' do stuff
      Range("S" & mr).Rows.EntireRow.Insert
      Range("I" & mr) = Range("S" & mr + 1)
      Range("A" & mr) = Range("A" & mr + 1)
      Range("B" & mr) = Range("B" & mr + 1)
      'adjust row
         r = mr
    End If
    'adjust for next row up
    r = r - 1
Loop

Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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