I'm pretty new to VBA and I've searched as best as I can but I still can't find an answer. I need to write a Macro that will insert a new line based on multiple conditions. The rows have to be in groups no larger than 5 and separated by carrier. But if a Container is repeating, it counts as 1 row.
Current:
Container Carrier
ABC56 Carrier 1
XOS752 Carrier 1
IOW45 Carrier 1
WOFJ74 Carrier 1
NMC85 Carrier 1
DDJD7 Carrier 1
DFF789 Carrier 1
DFF789 Carrier 1
CSGS Carrier 1
GSW132 Carrier 1
WYWI78 Carrier 1
WTS758 Carrier 1
MNV74 Carrier2
ADS78 Carrier2
CTDS45 Carrier2
CTDS45 Carrier2
LHKGL78 Carrier2
XJSS772 Carrier2
XJSHS7 Carrier2
OIJS7 Carrier2
Desired:
ABC56 Carrier 1
XOS752 Carrier 1
IOW45 Carrier 1
WOFJ74 Carrier 1
NMC85 Carrier 1
DDJD7 Carrier 1
DFF789 Carrier 1
DFF789 Carrier 1
CSGS Carrier 1
GSW132 Carrier 1
WYWI78 Carrier 1
WTS758 Carrier 1
MNV74 Carrier2
ADS78 Carrier2
CTDS45 Carrier2
CTDS45 Carrier2
LHKGL78 Carrier2
XJSS772 Carrier2
XJSHS7 Carrier2
OIJS7 Carrier2
I will take any direction you have! I have these two codes separately. One Separates by Carrier and One Separates into 5 row increments. However, It doens't have all the logic built in.
To separate into groups of 5:
Option Explicit
Sub InsertIT()
Dim x As Integer
x = 1 'Start Row
Do
Range("A" & x, "B" & x).Insert
x = x + 6
Loop
End Sub
To separate by Carrier:
Sub InsertRowAtChangeInValue()
For lRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 2 Step -1
If Cells(lRow, "B") <> Cells(lRow - 1, "B") Then Rows(lRow).EntireRow.Insert
Next lRow
End Sub
I copied your sample data, and this macro gives me the output you're looking for.
I used a
whileloop instead of aforloop because VBA records the value for the end of theforloop when it starts, and the number of rows you need to process changes as you insert rows.I'm using the concept of a counter that increments only when conditions are met to account for the repeat container and carrier rows.
I'm also using the concept of flag-setting to take the correct action when a carrier change is detected. As you learn and grow in writing vba, if you choose to use flags, remember to reset them as necessary as I've done here.
Finally, I included the user message at the end as a quick cognitive check for the functionality of the macro. Based on the user message, you can quickly scroll to the row indicated and check whether the macro processed the whole sheet. I find it helpful to include these messages to check my work and help my users catch errors.
If you have questions, please comment!