CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Kaustubh Katdare • 27 Jun, 2016
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.
@Aashish Joshi • 02 Jul, 2016 • 1 like 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;
@Kaustubh Katdare • 02 Jul, 2016 Python! Thanks a lot, @Aashish Joshi . 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. 😀
@Aashish Joshi • 03 Jul, 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. 😁
1.8k views

Related Posts

@pratap singh, upendra · Oct 17, 2013

Hi, what could be the physical significance of the thermal voltage encountered in semiconductor devices. Does it's physical significance change on changing the device under test. regards, proffy
3.8k views

@Anoop Mathew · Dec 2, 2014

It was much better back before 2000 when we used to study India with fewer states. Now, just for my reference, and also for everyone else who's finding it hard...
4.1k views

@Ankita Katdare · Feb 19, 2015

ISRO's Mangalyaan or Mars Orbiter Mission has been a topic of regular discussion over the last couple of years. Right from November 2013, when Indians wished it a 'happy journey',...
1.8k views

@Kaustubh Katdare · Aug 23, 2010

A simple good bye email or last working day mail ain't cool! We've some awesome templates that you can use. If you're looking for an amazing Last Working Day email...
775.9k views

@Radhika Deshpande · Dec 30, 2016

Abhinav Asthana grew up in small towns across Uttar Pradesh, thanks to his civil engineer father who worked at the Public Works Department in a job that demanded frequent transfers....
2.8k views