Inserting Rows Based on Change in Value in Column

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm coding a macro that will insert a row where the text changes from one value to another. Here is a example:

NameDepartmentShift
MyahOF10A
KenigiaOF10A
NettaOF10A
KendallOF10A
AndrewOF10A
DonnieOF10B
SummerOF10B
ShanikaOF10B
AnthonyOF10B
KyleOF12A
DustinOF12A
ArielleOF12A
TonyRec12B
JoshLDPA12B
JacobOF12B
JamesOF12B

I want to insert a row where the Shift value switches from 10A to 10B, 10B to 12A, and 12A to 12B, thus creating separation between the shifts. This is how it should look afterward:

NameDepartmentShift
MyahOF10A
KenigiaOF10A
NettaOF10A
KendallOF10A
AndrewOF10A
DonnieOF10B
SummerOF10B
ShanikaOF10B
AnthonyOF10B
KyleOF12A
DustinOF12A
ArielleOF12A
TonyRec12B
JoshLDPA12B
JacobOF12B
JamesOF12B

Does anyone have any suggestions?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:
VBA Code:
Sub InsertRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = LastRow To 3 Step -1
        If Cells(x, 3) <> Cells(x - 1, 3) Then
            Rows(x).Insert
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This is what I got:

NameDepartmentShift

MyahOF10A
KenigiaOF10A
NettaOF10A
KendallOF10A
AndrewOF10A
DonnieOF10B
SummerOF10B
ShanikaOF10B
AnthonyOF10B
KyleOF12A
DustinOF12A
ArielleOF12A

TonyRec12B

JoshLDPA12B

JacobOF12B
JamesOF12B

So it inserted rows between 12A and 12B, and then separated the different departments in 12B. And it inserted a row between the headers and the first row from 10A.
 
Upvote 0
This is what I got:
Book3
ABC
1NameDepartmentShift
2MyahOF10A
3KenigiaOF10A
4NettaOF10A
5KendallOF10A
6AndrewOF10A
7
8DonnieOF10B
9SummerOF10B
10ShanikaOF10B
11AnthonyOF10B
12
13KyleOF12A
14DustinOF12A
15ArielleOF12A
16
17TonyRec12B
18JoshLDPA12B
19JacobOF12B
20JamesOF12B
Sheet1

Are you using the macro on the data you posted?
 
Upvote 0
Oops, I realized my mistake. I left out a column in my example, so I changed the code to make everything adjust the 4th column instead of the 3rd. Now it works perfectly. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,215,167
Messages
6,123,401
Members
449,098
Latest member
ArturS75

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