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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
@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,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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