CrazyEngineers
  • Excel Formula To Create Abbreviations Using First Letters Of Text

    Kaustubh Katdare

    Administrator

    Updated: Oct 26, 2024
    Views: 1.4K
    I'm looking for an excel formula that will help me create abbreviations using the first letters of the text in columns -

    TEXT ------------------------- ---+------ABBREVIATION
    Stanford School of Engineering | SSE
    Wollongong University Dubai | WUD

    and so on.

    Note that the abbreviation excluded the 'of' while creating abbreviation. I'm aware that this can be done by using macros; and have already tried the following code which doesn't work (I keep getting error):

    Function GetInitials(ByVal s As String) As String
    Dim sExclude As String
    
    sExclude = "the|for|a|an|of" ' words to exclude
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
    
        .Pattern = "\b(" & sExclude & ")\b"
        s = .Replace(s, "")
    
        .Pattern = "\s*([a-z])[a-z]+\s*"
        GetInitials = UCase(.Replace(s, "$1"))
    
    End With
    End Function
    
    I've absolutely no experience with Macros and vB scripting. Would appreciate if anyone can help.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Aashish Joshi

    MemberJul 2, 2016

    Not sure about Excel but it is fairly easy to do this with Perl. 😀

    All you need to do is copy the "full form" column to Notepad and save it as a csv. Give the file's path to perl and let it run its magic 😀

    the drawback with this script is if any letter that needs to be ignored is capital it'll add that to the final abbreviation. Like Indian Institute Of Technology will become IIOT. However, with another condition in the if statement it should be possible to avoid this as well.

    I've attached 2 sample files that I tested the script with. Once you have the final abbreviated.csv just open it in Excel and copy/paste the contents.

    Here's a sample script:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Text::CSV;
    
    #name of the file where the full forms are kept
    my $fullform = '/path/to/full_name.csv';
    open my $read,"<",$fullform or die $!;
    
    #name of the file where the abbreviations need to be added
    my $abbreviation = '/path/to/abbreviated.csv';
    open my $write,">",$abbreviation or die $!;
    
    # if the first line in the csv is column name
    # uncomment the line below.
    #
    #my $junk = <$fh>;
    
    my $csv = Text::CSV->new();
    
    while(my $line = $csv->getline($read)){
       
        my $abb;
        my @split = split(" ",$line->[0]);
       
        my $len = $#split;
       
        for (0..$len){
            my $char = substr($split[$_],0,1);
            # if the first character is capital process it
            # otherwise do nothing
            if($char =~ m/[A-Z]/){
               
                # in the first run $abb is not
                # defined so make it = $char
                # from 2nd run onwards concatenate it.
                if(!defined $abb){
                    $abb = $char;
                }else{
                    $abb = $abb.$char;
                }
            }
        }
        print $write "\"$line->[0]\",\"$abb\"\n";
    }
    
    close $read;
    close $write;
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJul 2, 2016

    Python! Thanks a lot, #-Link-Snipped-# . This would definitely be handy. Btw, the macro I posted in my first post actually worked fine. It omits the dots (periods) but otherwise, it works just fine. I'm thinking of learning regex; to make life easier. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Aashish Joshi

    MemberJul 2, 2016

    Kaustubh Katdare
    I'm thinking of learning regex; to make life easier. 😀
    Oh yes, it makes things soo much easier. I've barely started learning regex and I can already see how easily it gets me out of sticky situations. 😁
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register