?
Solved

Excel Macro to break up a series of accounts

Posted on 2014-10-03
7
Medium Priority
?
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
Glenn Ray earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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