r/excel • u/man-teiv 226 • Feb 13 '18
User Defined Function I've made the =NATO() function, converting any given string into Nato phonetic alphabet!
I was wondering if such a thing existed and I realized I needed it in my life. I'm sure there are thousands of ways to improve it, so feel free to correct me on it! (EDIT: a much better version is shown in the comments)
Option Explicit
Function NATO(command As String)
Dim i#
Dim word As String
NATO = ""
For i = 1 To Len(command)
Select Case LCase(Mid(command, i, 1))
Case " "
word = ""
Case "a"
word = "Alfa"
Case "b"
word = "Bravo"
Case "c"
word = "Charlie"
Case "d"
word = "Delta"
Case "e"
word = "Echo"
Case "f"
word = "Foxtrot"
Case "g"
word = "Golf"
Case "h"
word = "Hotel"
Case "i"
word = "India"
Case "j"
word = "Juliett"
Case "k"
word = "Kilo"
Case "l"
word = "Lima"
Case "m"
word = "Mike"
Case "n"
word = "November"
Case "o"
word = "Oscar"
Case "p"
word = "Papa"
Case "q"
word = "Quebec"
Case "r"
word = "Romeo"
Case "s"
word = "Sierra"
Case "t"
word = "Tango"
Case "u"
word = "Uniform"
Case "v"
word = "Victor"
Case "w"
word = "Whiskey"
Case "x"
word = "X-ray"
Case "y"
word = "Yankee"
Case "z"
word = "Zulu"
End Select
NATO = NATO & word & " "
Next i
End Function
131
Upvotes
14
u/Travyplx Feb 13 '18
Just a minor point to bring up, while Alpha would be correct for the U.S. military... it is actually spelled 'Alfa' for NATO.
27
u/pancak3d 1189 Feb 13 '18 edited Feb 13 '18
Nice. There are ways to make the code shorter and more efficient, sure, but this approach is perfectly good for the application and well written!
3
u/blkmmb Feb 13 '18
I might be super dumb with this question, but I have added these lines to the VBA editor but I can't call the Function in my sheets it returns nothing.
Am I missing something?
2
u/rvba 3 Feb 14 '18 edited Feb 14 '18
The idea is good... but the execution is so damn poor. I really do not understand why this function even got 125 upvotes - and only one guy even bothered to rewrite this code to make it shorter. I guess only few people here write actual production quality code, not hacky solutions that work in short term.
Constructive review of this code:
In fact SELECT CASE is one of the reasons why programmers of other languages laugh at Excel code, because if you had few such functions updating them could become very problematic.
2) Your function has no error correction, what makes it simply NOT WORK as advertised. If someone has a text string like for example "ABC123", only the letters will be changed, the numbers will be deleted!!! This problem would not exist if you had a very simple error handler: when the character does not exist in your gigantic dictionary (ehem SELECT CASE), then use the charcter itself. This way your function would work with numbers, special characters (like comma or exclamation mark), or languages other than English. Now your function just deletes stuff - you didnt even write an opening comment at the beginning to explain this behavor!
As an excercise you can add a parameter to your function, to toggle delting / leaving non US-alphabet characters. So it would be like =NATO(A1;0) - to keep and =NATO(A1;1) to delete
Forgetting about error correction is something that novice programmers do often. You should either have some system to handle errors, or just warn the user - function could return something like "Error:120 wrong source data".
3) Testing/quality assurance. This is actually hard to implement in Excel - and that's why Excel gets so much criticism of programmers of 'modern languages'. You did not provide any spreadsheet to test the function. In fact, in theory, some debugging code could be commented out and used to test if results are as planned (e.g. second function that passes a string ABCDEFG... to function NATO and then compares the result with a hardcoded correct result, although here we could argue if it wouldnt be better to keep the debug code in your original function - and maybe comment it out for performance).
It's hard to find typos, so one should always think ahead and prepare for such debugging (some other user pointed out that you had Alpha instead of Alfa).
Solution presented by /u/pancak3d might be harder to read by basic user, but is much cleaner. Sadly it also has similar problems and a simple text string like ABC123 defeats it, since it does not parse anything else than american letters.
12
u/jfurt16 1 Feb 13 '18
just as an FYI - I believe Nato switched back to the use of Mancy for "m" in 2016
1
u/Selkie_Love 36 Feb 13 '18
I think option Text compare would have helped with this code. Also, dictionaries:
https://excelmacromastery.com/vba-dictionary/
3
u/FiscalFrontier 1 Feb 13 '18
This is the Far Side of Excel VBA. Thanks for sharing!
3
u/thergmguy Feb 14 '18
November India Charlie Echo Juliett Oscar Bravo!
2
u/excelevator 3071 Feb 14 '18 edited Feb 14 '18
...---...We want MORSE CODE!!! :)challenge accepted
2
u/Kingpin-3 Apr 08 '18
I just had this exact problem. I have 8 character strings, and I wanted to display the characters as phonetic words but case sensitive.
My solution was to use VLOOKUP with an array, but since VLOOKUP isn't case sensitive I needed to convert the characters to ASCII Code. Threw in some error handling too, to save listing every. single. character.
If anyone's interested I can post the code, kind've limited use, since you need to know the length of the string, and it's not very pretty. No doubt someone could improve on it.
1
u/tirlibibi17 Feb 13 '18
This is nice. Helps when dealing with support staff over the phone.
1
u/Limebaish Feb 13 '18
Amazing idea. Thanks for sharing :)
0
u/strickzilla Feb 14 '18
"M" should be "Mancy"