Solved

Excel Macro to break up a series of accounts

Posted on 2014-10-03
7
160 Views
Last Modified: 2014-10-03
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

0
Comment
Question by:jnsimex
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40359934
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
 

Author Comment

by:jnsimex
ID: 40359986
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
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40360325
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:jnsimex
ID: 40360336
Yes please.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40360492
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
 

Author Closing Comment

by:jnsimex
ID: 40360569
It worked like a champ. Thank you!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40360619
You're welcome.  Have a great weekend.

-Glenn
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question