Excel Macro to break up a series of accounts

Hello Experts, I have a series of accounts that I would like separated in two groups. As you can see in the code section, if the account number ends in either 900, 950 or 999 then I would like to insert a couple of empty rows above 900 to create separation. The problem is not all the account series will have these 3 numbers, some will only have 900 or 950 or 999 or  2 of the 3 numbers. So if the account series has accounts 950 and 999, I would like to add the empty rows above 950. If the account series only has account 999,  then i would like to add the empty rows above 999.

    
    14000HELI100 
    14000HELI105 
    14000HELI110 
    14000HELI200 
    14000HELI205 
    14000HELI215 
    14000HELI400 
    14000HELI405 
    14000HELI470 
    14000HELI505 
    14000HELI510 
    14000HELI525 
    14000HELI605 
    14000HELI630 
    14000HELI635 
    14000HELI650 
    14000HELI700 
    14000HELI705 
    14000HELI710 
    14000HELI715 
    14000HELI720 
    14000HELI800 
    14000HELI870 
    14000HELI900 
    14000HELI950 
    14000HELI999 

Open in new window

jnsimexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glenn RayExcel VBA DeveloperCommented:
Hi, this VBA code will insert a blank row before the 900-series accounts:
Option Explicit
Sub Split_900_series()
    Dim rng As Range
    Dim cl As Object
    
    Set rng = Range("A1", Range("A1").End(xlDown))
    For Each cl In rng
        If Mid(cl.Value, Len(cl.Value) - 2, 1) = "9" Then
            cl.EntireRow.Insert
            Exit Sub
        End If
    Next cl
End Sub

Open in new window


The code presumes that there is a continuguous set of accounts (i.e., no empty cells/rows in the data) and that they are in column A.

An example workbook is attached.  To test, press [Alt]+[F8] then select "Split_900_Series" from the list.
EE-Q-28530843.xlsm
0
jnsimexAuthor Commented:
hi Glenn, thanks for your quick response. the macro worked great but how can i get this macro to work if i had another series of accounts that follows the first one? and those accounts contained 900 accounts.

    14000HELI100 
    14000HELI105 
    14000HELI110 
    14000HELI200 
    14000HELI205 
    14000HELI215 
    14000HELI400 
    14000HELI405 
    14000HELI470 
    14000HELI505 
    14000HELI510 
    14000HELI525 
    14000HELI605 
    14000HELI630 
    14000HELI635 
    14000HELI650 
    14000HELI700 
    14000HELI705 
    14000HELI710 
    14000HELI715 
    14000HELI720 
    14000HELI800 
    14000HELI870 
    14000HELI900 
    14000HELI950 
    14000HELI999 
    14000OSHV100 
    14000OSHV105 
    14000OSHV210 
    14000OSHV400 
    14000OSHV505 
    14000OSHV520 
    14000OSHV600 
    14000OSHV605 
    14000OSHV630 
    14000OSHV650 
    14000OSHV655 
    14000OSHV700 
    14000OSHV710 
    14000OSHV715 
    14000OSHV716 
    14000OSHV720 
    14000OSHV730 
    14000OSHV800 
    14000OSHV900 
    14000OSHV950 
    14000OSHV999 

Open in new window

0
Glenn RayExcel VBA DeveloperCommented:
The logic here is different.  Do you want a blank row before AND AFTER the 900-series accounts like so?
    14000HELI100 
    14000HELI105 
    14000HELI110 
    14000HELI200 
    14000HELI205 
    14000HELI215 
    14000HELI400 
    14000HELI405 
    14000HELI470 
    14000HELI505 
    14000HELI510 
    14000HELI525 
    14000HELI605 
    14000HELI630 
    14000HELI635 
    14000HELI650 
    14000HELI700 
    14000HELI705 
    14000HELI710 
    14000HELI715 
    14000HELI720 
    14000HELI800 
    14000HELI870 

    14000HELI900 
    14000HELI950 
    14000HELI999 

    14000OSHV100 
    14000OSHV105 
    14000OSHV210 
    14000OSHV400 
    14000OSHV505 
    14000OSHV520 
    14000OSHV600 
    14000OSHV605 
    14000OSHV630 
    14000OSHV650 
    14000OSHV655 
    14000OSHV700 
    14000OSHV710 
    14000OSHV715 
    14000OSHV716 
    14000OSHV720 
    14000OSHV730 
    14000OSHV800 

    14000OSHV900 
    14000OSHV950 
    14000OSHV999 

Open in new window


-Glenn
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jnsimexAuthor Commented:
Yes please.
0
Glenn RayExcel VBA DeveloperCommented:
Sorry for the delay.  Okay, here's the modified code and updated workbook.  A different logic was needed to process this.
Option Explicit
Sub Split_Accts()
    Dim rng As Range
    Dim cl As Object
    Dim strAcct As String
    Dim bool900Series As Boolean
    
    'First pass:  Split by accounts
    Set rng = Range("A1", Range("A" & Cells.Rows.Count).End(xlUp).Offset(-1, 0))
    For Each cl In rng
        If cl.Value = "" Then
            strAcct = Left(cl.Offset(1, 0).Value, Len(cl.Offset(1, 0).Value) - 3)
        Else
            strAcct = Left(cl.Value, Len(cl.Value) - 3)
        End If
        If strAcct <> Left(cl.Offset(1, 0).Value, Len(cl.Offset(1, 0).Value) - 3) Then
            cl.Offset(1, 0).EntireRow.Insert
        End If
    Next cl
    
    'Second pass: Split out 900 series
    Set rng = Range("A1", Range("A" & Cells.Rows.Count).End(xlUp).Offset(-1, 0))
    For Each cl In rng
        If cl.Value <> "" And bool900Series = False Then
            If Mid(cl.Value, Len(cl.Value) - 2, 1) = "9" Then
                cl.EntireRow.Insert
                bool900Series = True
            Else
                bool900Series = False
            End If
        Else
            If cl.Value = "" Then bool900Series = False
        End If
    Next cl
End Sub

Open in new window

EE-Q-28530843.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jnsimexAuthor Commented:
It worked like a champ. Thank you!
0
Glenn RayExcel VBA DeveloperCommented:
You're welcome.  Have a great weekend.

-Glenn
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.