Solved

Next letter in sequence

Posted on 2014-11-26
6
118 Views
Last Modified: 2014-11-27
Hi All,

I have a report in Excel 2010 which summarises costs against certain activity charge codes. The charge codes for this particular project are sequential and always 8 alpha characters. They all start with the same 4 characters and then end with AAAA, moving on to AAAB, AAAC etc. I also use this list to indicate which activity charge code is needed next in the sequence and so far this works.

The summary list has a column that pulls the last character for each of the activities, populating a range G2:G26. I then use the following formula to determine what character I need next:

=CHAR(MAX(CODE(G2:G26))+1)  entered as an array formula.

My list of last characters is not in alphabetical order so it isn't as simple as looking at the last one in the list. The current list has reached Y so the next activity to be raised will end in AAAZ. Thats fine, my formula currently returns Z as the correct answer.

However when I go beyond Z, the next activity will have to end AABA, then AABB etc.

A sample of the set up is attached. You will see from the sample that some tasks have multiple phases (preparation and execution (P / E)) and each phase has a new activity code, hence why they are not in alphabetical order. Some tasks were raised in original group and then others have been added.

The formula that I am trying to update is highlighted. I am happy to change the formulas to pull out the existing suffixes if required, hence why I have supplied the full suite of data as best I can.

At the moment I can do the increment simply but as this project expands it will become less simple.

Thanks
Rob H
Sequential-lettering.xlsx
0
Comment
Question by:Rob Henson
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40467874
I couldn't think of a way to do this with a formula, while that does not mean it cannot be done, here is a VBA function to do it for you

Function LetterIncrement(stringCounter As String) As String
    Dim arr(1 To 4) As Integer
    Dim i As Integer
    
    For i = 1 To 4
        arr(i) = Asc(UCase(Mid(stringCounter, i, 1)))
    Next
    
    arr(4) = arr(4) + 1
    
    For i = 4 To 1 Step -1
        If (arr(i) > 90) Then
            arr(i) = 65
            arr(i - 1) = arr(i - 1) + 1
        End If
    Next
    
    LetterIncrement = Chr(arr(1)) & Chr(arr(2)) & Chr(arr(3)) & Chr(arr(4))
    
End Function

Open in new window


Note:
I deliberately let it throw an error if "ZZZZ" is entered
Always returns upper case but will take upper or lower as an argument
0
 
LVL 31

Author Comment

by:Rob Henson
ID: 40468013
Thanks I will check it out when back at desk tomorrow.

Question though, this looks like it needs an input of 4 letters in a string and it will give the next string of 4. How do I determine the string of 4 to enter? I can change my list so that it shows 4 characters rather than 1 but how can I check the list for the "highest" and thus get the next.

Thanks
0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 40468075
It is possible to make an array formula to do it.
I based it on the Charge code column D on sheet Data, looking at the range G2:G1000.

=CHAR(MOD(INT((MAX((IFERROR(CODE(MID($G2:$G$1000,5,1))-65,0))*26^3+(IFERROR(CODE(MID($G2:$G$1000,6,1))-65,0))*26^2+(IFERROR(CODE(MID($G2:$G$1000,7,1))-65,0))*26+IFERROR(CODE(MID($G2:$G$1000,8,1))-65,0))+1)/26^3),26)+65)&CHAR(MOD(INT((MAX((IFERROR(CODE(MID($G2:$G$1000,5,1))-65,0))*26^3+(IFERROR(CODE(MID($G2:$G$1000,6,1))-65,0))*26^2+(IFERROR(CODE(MID($G2:$G$1000,7,1))-65,0))*26+IFERROR(CODE(MID($G2:$G$1000,8,1))-65,0))+1)/26^2),26)+65)&CHAR(MOD(INT((MAX((IFERROR(CODE(MID($G2:$G$1000,5,1))-65,0))*26^3+(IFERROR(CODE(MID($G2:$G$1000,6,1))-65,0))*26^2+(IFERROR(CODE(MID($G2:$G$1000,7,1))-65,0))*26+IFERROR(CODE(MID($G2:$G$1000,8,1))-65,0))+1)/26),26)+65)&CHAR(MOD((MAX((IFERROR(CODE(MID($G2:$G$1000,5,1))-65,0))*26^3+(IFERROR(CODE(MID($G2:$G$1000,6,1))-65,0))*26^2+(IFERROR(CODE(MID($G2:$G$1000,7,1))-65,0))*26+IFERROR(CODE(MID($G2:$G$1000,8,1))-65,0))+1),26)+65)

Open in new window


See J2 on sheet Data.
It is not as complicated as it looks, see below cells on the sheet.
But when assembled into one formula it gets long, because the calculation for the max value is done 4 times.
Sequential-lettering.xlsx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:Michael74
ID: 40468134
Here is an update.
LetterIncremented takes a range
Determines the largest value in the range
Then returns the next number with leading "A"s stripped.

It has been done by treating the string as a Base 26 number with "A" as zero.

Private Function Base26Max(rng As Range) As String
    Dim r As Range
    Dim i As Long, j As Long, currMax As Long
    Dim arr(1 To 4) As Integer
    Dim str As String
    
    currMax = 0
        
    For Each r In rng
        str = Right(r.Value, 4)
        j = 0
        For i = 1 To 4
            j = j + ((Asc(UCase(Mid(str, i, 1))) - 65) * (26 ^ (i - 1)))
        Next
        If j > currMax Then Base26Max = str
    Next
    
End Function

Function LetterIncremented(rng As Range) As String
    Dim arr(1 To 4) As Integer
    Dim i As Integer
    Dim str As String
    
    str = Base26Max(rng)
    
    For i = 1 To 4
        arr(i) = Asc(UCase(Mid(str, i, 1)))
    Next
    
    arr(4) = arr(4) + 1
    
    For i = 4 To 1 Step -1
        If (arr(i) > 90) Then
            arr(i) = 65
            arr(i - 1) = arr(i - 1) + 1
        End If
    Next
    
    If arr(1) = 65 Then
        If arr(2) = 65 Then
            If arr(3) = 65 Then
                LetterIncremented = Chr(arr(4))
            Else
                LetterIncremented = Chr(arr(3)) & Chr(arr(4))
            End If
        Else
            LetterIncremented = Chr(arr(2)) & Chr(arr(3)) & Chr(arr(4))
        End If
    Else
        LetterIncremented = Chr(arr(1)) & Chr(arr(2)) & Chr(arr(3)) & Chr(arr(4))
    End If
    
End Function

Open in new window

0
 
LVL 31

Author Closing Comment

by:Rob Henson
ID: 40468692
Ejgil, the array formula works great. I have been able to successfully transfer it to the proper file with a couple of amendments to that file and it works well.

Michael74 - unfortunately I couldn't get the Function method to work and I didn't really want to go down the VBA route, issues with converting to xlsm and other users not being able to view from server or sharepoint sites etc.

Thanks to both
Rob H
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40469677
All good Rob. Wasn't sure if it could be done with a formula and it sounded like it would be fun to do in VBA. I am glad Ejgil could give you the solution you needed
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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

747 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

14 Experts available now in Live!

Get 1:1 Help Now