r/excel • 226 • Feb 13 '18

User Defined Function I've made the =NATO() function, converting any given string into Nato phonetic alphabet!

And here's the result

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

61 comments sorted by

View all comments

Show parent comment

9

u/Bernard_schwartz Feb 13 '18

Can someone give an example of how to make it shorter and more efficient?

44

u/pancak3d 1189 Feb 13 '18 edited Feb 14 '18
Option Explicit

Function NATO(command As String)
    Dim i#, num#
    Dim word As String
    Dim NatoAlpha() As String
    
    NatoAlpha = Split("Alpha,Bravo,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,Indigo,Juliett,Kilo,Lima,Mike,November,Oscar,Papa,Quebec,Romeo,Sierra,Tango,Uniform,Victor,Whiskey,Xray,Yankee,Zulu", ",")
    NATO = ""
    For i = 1 To Len(command)
        num = Asc(UCASE(Mid(command, i, 1))) - 65
        If num >= 0 And num <= 25 Then
            NATO = NATO & NatoAlpha(num) & " "
        End If
    Next i
    NATO = Trim(NATO)
    
End Function

Could make it even shorter by adding an On Error Resume Next and skipping the IF statement

12

u/man-teiv 226 Feb 13 '18

Oooh that's nice

3

u/Bernard_schwartz Feb 13 '18

Oh wow. Cool. Thanks.

3

u/sooka 43 Feb 13 '18

I can confirm that this one is more efficient :D
I've computed 1 milion iterations of each for the string NATONATONATONATONATONATONATONATONATONATO, here are the results:

NATO functionNr. of iterationsSeconds to complete
OP (man-teiv)1.000.00036,9767674935083
Bernard_schwartz1.000.00023,2220450558661

method used here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/excel-improving-calcuation-performance?f=255&MSPPError=-2147217396

the code:

#If VBA7 Then
    Private Declare PtrSafe Function getFrequency Lib "kernel32" Alias _
        "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
    Private Declare PtrSafe Function getTickCount Lib "kernel32" Alias _
         "QueryPerformanceCounter" (cyTickCount As Currency) As Long
#Else
    Private Declare Function getFrequency Lib "kernel32" Alias _
        "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
    Private Declare Function getTickCount Lib "kernel32" Alias _
        "QueryPerformanceCounter" (cyTickCount As Currency) As Long
#End If
Function MicroTimer() As Double
'

' Returns seconds.
    Dim cyTicks1 As Currency
    Static cyFrequency As Currency
    '
    MicroTimer = 0

' Get frequency.
    If cyFrequency = 0 Then getFrequency cyFrequency

' Get ticks.
    getTickCount cyTicks1

' Seconds
    If cyFrequency Then MicroTimer = cyTicks1 / cyFrequency
End Function

Sub test_nato()

    dTime = MicroTimer
    For i = 1 To 1000000
        nato_op ("NATONATONATONATONATONATONATONATONATONATO")
    Next i
    
    Cells(2, 10).Value2 = MicroTimer - dTime
    
    dTime = MicroTimer
    For i = 1 To 1000000
        nato_bernard ("NATONATONATONATONATONATONATONATONATONATO")
    Next i
    
    Cells(2, 11).Value2 = MicroTimer - dTime

End Sub

3

u/JPDVP 48 Feb 13 '18

Just one warning. Different machines/software versions might have implications on the execution time of the code.

Most of us nowadays I guess use similar processors and operating systems but for example in my machine (which is not that great) I got similar results but with a lot less difference in between cases (29 vs 23)

3

u/sooka 43 Feb 13 '18

Absolutely correct, I should have added a "your mileage may vary".

1

u/JPDVP 48 Feb 13 '18

I found it really interesting that in mine the "best" option didn't have a different mileage but the worst clearly had.

1

u/sooka 43 Feb 13 '18

Yup, I don't get why though.
I run it multiple times and I got really similar results every time.
Even tried inverting the functions (Bernard first and OP second) and had basically the same results as before.

Don't know where your 29, that's much lower than 36, comes from.
I'm on Excel Pro Plus 2016 1801 (9001.2144), will try with O365 but it will be a totally different machine.
This one is a i7 4770 btw.

1

u/JPDVP 48 Feb 13 '18

2.5 years old laptop (I would consider mid range when bought) i7 as well (dont exactly the model)

Running O365 64 bit (maybe that is the difference?)

I tested 4 times (couldn't be bothered) and got always similar results

1

u/sooka 43 Feb 13 '18

Could really be that O365 optimized the Select Case, I'll try that and report back just for the sake of it :D.

I'm also curious how a C# Parallel.ForEach will perfom, both so and as an UDF. Will try, so in the meantime I'll learn how to create UDFs in C#.

→ More replies (4)

1

u/pancak3d 1189 Feb 13 '18 edited Feb 13 '18

I got 49 seconds and 30 seconds. Office 2016 32-bit. Almost the exact same ratio as /u/sooka but just on my slower processor!

Aren't "Office 365" and Excel 2016 both the same software? I thought Office 365 is just a subscription that provides you with 2016 (and beyond) Excel software.

1

u/pancak3d 1189 Feb 13 '18

Lol thanks. I'm guessing calling SPLIT every single time is slowing it down. Could have declared a public array up front instead to shave this time down.

2

u/sooka 43 Feb 13 '18 edited Feb 13 '18

:D
you gained some, timing is now: 17,1745068816654
Declaring your i and num as integer instead of double get you some more: 15,6757106032856
and NATO = vbNullString instead of NATO = "" will save a liiiiitle more: 15,6092395105661

Dim NatoAlpha() As String

Sub test_natoAlpha()
          
        NatoAlpha = Split("Alpha,Bravo,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,Indigo,Juliett,Kilo,Lima,Mike,November,Oscar,Papa,Quebec,Romeo,Sierra,Tando,Uniform,Victor,Whiskey,Xray,Yankee,Zulu", ",")  

        dTime = MicroTimer
        For i = 1 To 1000000
            nato_bernard ("NATONATONATONATONATONATONATONATONATONATO")
        Next i
        
        Cells(2, 13).Value2 = MicroTimer - dTime
    
End Sub

Function nato_bernard(command As String)

    Dim i As Integer, num As Integer
    Dim word As String
    Dim NATO As String

    NATO = vbNullString
    For i = 1 To Len(command)
        num = Asc(UCase(Mid(command, i, 1))) - 65
        If num >= 0 And num <= 25 Then
            NATO = NATO & NatoAlpha(num) & " "
        End If
    Next i
    NATO = Trim(NATO)

End Function

edit: by not evaluating the parameter passed (parentheses around the parameter IIRC) we can get a 15,0289548254968

2

u/pancak3d 1189 Feb 13 '18

Note to self, tag /u/sooka whenever we need macro execution time compared

2

u/ubbm 38 Feb 14 '18

Try setting NatoAlpha to static and only Split() if its empty. This should speed it up even more.

Static NatoAlpha() as String

If Not NatoAlpha Then NatoAlpha = Split("Alpha,Bravo,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,Indigo,Juliett,Kilo,Lima,Mike,November,Oscar,Papa,Quebec,Romeo,Sierra,Tando,Uniform,Victor,Whiskey,Xray,Yankee,Zulu", ",")  

1

u/sooka 43 Feb 14 '18

NatoAlpha is already out of the 1 mil iterations. It doesn't get counted in anymore.
But maybe I dnd't understand it correctly, if so point me to the right direction; I'll implement it.

1

u/man-teiv 226 Feb 15 '18

Ahahahah, some QA! On my crappy function! I love it!

1

u/Selkie_Love 36 Feb 13 '18

1

u/pancak3d 1189 Feb 13 '18

Oh that's really interesting, did not know that was possible. This would work for OP's function but not for the one I posted -- in my case UCASE is needed to get the correct value from the Asc function (Asc will return different numbers for upper and lowercase characters)

1

u/Selkie_Love 36 Feb 13 '18

Makes sense - I had skimmed your function super fast

1

u/Autoradiograph Feb 13 '18

Could make it even shorter by adding an On Error Resume Next and skipping the IF statement

OMG, don't even think of doing such things. Brevity is not the primary goal of code, and On Error Resume Next is cancer.

3

u/pancak3d 1189 Feb 13 '18

Agree to disagree -- On Error Resume Next is quite useful when you're expecting a specific error and want to ignore/bypass it. It can be (and is) used in perfectly acceptable code.

On the other hand, if you're using it to just say "IDK what's going to happen but I don't want to see any errors", that's obviously not a good approach.

2

u/phranticsnr 1 Feb 14 '18

I just had that problem yesterday, using items.restrict() on mailboxes that have more than MailItems in them. I didn't want to if/then or select/case every possible object type.. on error resume next just skips over any item that borks the loop.

1

u/ambition1 Feb 14 '18

*Tango.... Technical This is the first time I've corrected someones code. Ha