Create new row for duplicate cell values

albie91

New Member
Joined
Apr 22, 2021
Messages
6
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello,
I could use some help with a certain topic. I have an Excel sheet with multiple rows and columns. I sorted the rows based on the values in column A. I need to run a VBA script to loop through the worksheet so that whenever a row is found with the same value repeated in column A, a row would be generated on top of it, and the value of column A would be copied. The other cells in this new row shall be empty. So, if the input sheet is as follows:
NameProperty 1Property 2
ABC12A15B
ABC1415B
XYZ53
PQR34C10
PQR22A46B
PQR1545R
then, the output sheet would be as follows:
NameProperty 1Property 2
ABC
ABC12A15B
ABC1415B
XYZ53
PQR
PQR34C10
PQR22A46B
PQR1545R
It would also be helpful if the newly created rows (if any), are formatted in red color.
My main issue is, how do I ensure that if I re-run the script on the above output, a new row is not added over an already existing empty row.
So if I run the VBA script again, the following should NOT happen:
NameProperty 1Property 2
ABC
ABC
ABC12A15B
ABC1415B
XYZ53
PQR
PQR
PQR34C10
PQR22A46B
PQR1545R

Thank you for the time and help.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
My amateur VBA might do it until someone does a really slick one.

Code:
Sub SpecInsert()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = lr To 2 Step -1
 If (Cells(i, 1) <> Cells(i - 1, 1)) And (Not IsEmpty(Cells(i, 2))) And (WorksheetFunction.CountIf(Range("A1:A" & lr), Cells(i, 1)) > 1) And Not IsEmpty(Cells(i - 1, 1)) Then
 Cells(i, 1).EntireRow.Insert Shift:=xlShiftDown
 Cells(i, 1) = Cells(i + 1, 1)
 Else
 End If
Next i
End Sub
 
Upvote 0
Solution

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