Solved

Excel Macro to break up a series of accounts

Posted on 2014-10-03
7
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:jnsimex
Comment Utility
Yes please.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
Comment Utility
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
Comment Utility
It worked like a champ. Thank you!
0
 
LVL 27

Expert Comment

by:Glenn Ray
Comment Utility
You're welcome.  Have a great weekend.

-Glenn
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now