r/excel • u/excelevator 3071 • Feb 14 '18
User Defined Function I've made the =MORSEC() function, converting any given string into morse code!
Blatantly stolen from this post and this code , a Morse Code generator.
Function MORSEC(command As String)
Dim i#, num#
Dim word As String
Dim MORSECAlpha() As String
MORSECAlpha = Split(".-,-...,-.-.,-..,.,..-.,--.,....,..,.---,-.-,.-..,--,-.,---,.--.,--.-,.-.,...,-,..-,...-,.--,-..-,-.--,--..,-----,.----,..---,...--,....-,.....,-....,--...,---..,----.,/,", ",")
MORSEC = ""
For i = 1 To Len(command)
num = Asc(UCase(Mid(command, i, 1))) - 65
If num >= 0 And num <= 25 Then
MORSEC = MORSEC & MORSECAlpha(num) & " "
ElseIf num >= -17 And num <= -8 Then
MORSEC = MORSEC & MORSECAlpha(num + 43) & " "
ElseIf num = -33 Then
MORSEC = MORSEC & MORSECAlpha(num + 69) & " "
End If
Next i
MORSEC = Trim(MORSEC)
End Function| Text | Morse Code |
|---|---|
| Excel morse | =morsec(A2) |
| Excel morse | . -..- -.-. . .-.. / -- --- .-. ... . |
edit: I was going to use unicode • ➖ but early versions Excel do not have the UNICHAR function and it would have been a bitch to implement as I look now!
edit2: implement word spacer as per this comment below
edit3: 20190716: 1 thru 9 code reversed for correct output. doh! thankyou u/Lapin3d
105
Upvotes
4
u/JeronimoNL Feb 14 '18
2
u/d3skjet 49 Feb 14 '18
I'm unfamiliar with this syntax:
What's the deal?
32
u/small_trunks 1638 Feb 14 '18
Finally
15
u/user699 12 Feb 14 '18
The next logical function is to make it sound. This function takes the output of OP's function and uses beep to sound it out. Note the declarations at the top.
10
u/[deleted] Feb 14 '18
At my work this will be welcomed, as pushing the company forward into the 19th century!
4
u/man-teiv 226 Feb 14 '18
Lol I love you. I actually wanted to post it to reap double karma :P
4
u/sooka 43 Feb 14 '18
I'm timing your function OP, for 1.000.000 iterations with the string
MORSECMORSECMORSECMORSECMORSECMORSECMORSECMORSECMORSECMORSECit takes34,1151249923396sconds on my pc (i7 4770, Excel 2016). Your mileage may vary.After a slight optimization it takes
22,9485293163189seconds. Here is the code for the 2 tests (microsecond method here).So to recap:
1
u/farfromunique 3 Feb 15 '18
When I read the first half of the title, I thought this was going to be a joke post about making things "MORe SECure"
I was a bit disappointed.
1
u/FiscalFrontier 1 Feb 14 '18
I'm going on a cruise in two weeks... this should come in handy.
1
u/[deleted] Feb 14 '18
You wouldn't happen to be a ham radio guy...