I need help in making VB Excel macro that would take input from InputBox and convert it from English to Morse and vice versa, then show result in MessageBox. I've been stuck, and I've got no clue how could I make it. Thanks for help in advance <3
Asked
Active
Viewed 412 times
2 Answers
0
Please see the following (just for your reference - you may need to convert the codes to VBA, but I believe you can do it )
If txtInput.Text = "a" Then
lblStatus.Caption = ".-"
End If
If txtInput.Text = "b" Then
lblStatus.Caption = "-..."
End If
If txtInput.Text = "c" Then
lblStatus.Caption = "-.-."
End If
If txtInput.Text = "d" Then
lblStatus.Caption = "-.."
End If
If txtInput.Text = "e" Then
lblStatus.Caption = "."
End If
If txtInput.Text = "f" Then
lblStatus.Caption = "..-."
End If
If txtInput.Text = "g" Then
lblStatus.Caption = "--."
End If
If txtInput.Text = "h" Then
lblStatus.Caption = "...."
End If
If txtInput.Text = "i" Then
lblStatus.Caption = ".."
End If
If txtInput.Text = "j" Then
lblStatus.Caption = ".---"
End If
If txtInput.Text = "k" Then
lblStatus.Caption = "-.-"
End If
If txtInput.Text = "l" Then
lblStatus.Caption = ".-.."
End If
If txtInput.Text = "m" Then
lblStatus.Caption = "--"
End If
If txtInput.Text = "n" Then
lblStatus.Caption = "-."
End If
If txtInput.Text = "o" Then
lblStatus.Caption = "---"
End If
If txtInput.Text = "p" Then
lblStatus.Caption = ".--."
End If
If txtInput.Text = "q" Then
lblStatus.Caption = "--.-"
End If
If txtInput.Text = "r" Then
lblStatus.Caption = ".-."
End If
If txtInput.Text = "s" Then
lblStatus.Caption = "..."
End If
If txtInput.Text = "t" Then
lblStatus.Caption = "-"
End If
If txtInput.Text = "u" Then
lblStatus.Caption = "..-"
End If
If txtInput.Text = "v" Then
lblStatus.Caption = "...-"
End If
If txtInput.Text = "w" Then
lblStatus.Caption = ".--"
End If
If txtInput.Text = "x" Then
lblStatus.Caption = "-..-"
End If
If txtInput.Text = "y" Then
lblStatus.Caption = "-.--"
End If
If txtInput.Text = "z" Then
lblStatus.Caption = "--.."
End If
If txtInput.Text = "0" Then
lblStatus.Caption = "-----"
End If
If txtInput.Text = "1" Then
lblStatus.Caption = ".----"
End If
If txtInput.Text = "2" Then
lblStatus.Caption = "..---"
End If
If txtInput.Text = "3" Then
lblStatus.Caption = "...--"
End If
If txtInput.Text = "4" Then
lblStatus.Caption = "....-"
End If
If txtInput.Text = "5" Then
lblStatus.Caption = "....."
End If
If txtInput.Text = "6" Then
lblStatus.Caption = "-...."
End If
If txtInput.Text = "7" Then
lblStatus.Caption = "--..."
End If
If txtInput.Text = "8" Then
lblStatus.Caption = "---.."
End If
If txtInput.Text = "9" Then
lblStatus.Caption = "----."
End If

Ken Lee
- 6,985
- 3
- 10
- 29
-
Thank you so much, I'll try to make it tomorrow morning. :) – Saša Bugarin Dec 20 '20 at 19:00
-
You are welcome. Have a great day – Ken Lee Dec 21 '20 at 02:04
0
English to Morse Code
- Although this is almost by default done with a
Dictionary object
, I stuck with the arrays as an interesting alternative.
The Code
Option Explicit
Function getMorseCode( _
ByVal s As String, _
Optional ByVal CharDelimiter As String = " ", _
Optional ByVal WordDelimiter As String = vbLf, _
Optional ByVal NotFoundReplacement As String = "~") _
As String
Dim CharsList As String
CharsList = _
"a|b|c|d|e|f|g|h|i|j|" _
& "k|l|m|n|o|p|q|r|s|t|" _
& "u|v|w|x|y|z|" _
& "0|1|2|3|4|5|6|7|8|9|" _
& ".|,|?|:|/|""|'|;|!|" _
& "(|)|&|=|+|-|_|$|@|" _
& " "
Dim CodesList As String
CodesList = _
".-|-...|-.-.|-..|.|..-.|--.|....|..|.---|" _
& "-.-|.-..|--|-.|---|.--.|--.-|.-.|...|-|" _
& "..-|...-|.--|-..-|-.--|--..|" _
& "-----|.----|..---|...--|....-|.....|-....|--...|---..|----.|" _
& ".-.-.-|--..--|..--..|---...|-..-.|.-..-.|.----.|-.-.-.|-.-.--|" _
& "-.--.|-.--.-|.-...|-...-|.-.-.|-....-|..--.-|...-..-|.--.-.|" _
& WordDelimiter
Dim Chars() As String: Chars = Split(CharsList, "|")
'Debug.Print Join(Chars, vbLf)
Dim Codes() As String: Codes = Split(CodesList, "|")
'Debug.Print Join(Codes, vbLf)
Dim CurrentMatch As Variant
Dim n As Long
Dim cChar As String
Dim Result As String
For n = 1 To Len(s)
cChar = Mid(s, n, 1)
CurrentMatch = Application.Match(cChar, Chars, 0)
If IsNumeric(CurrentMatch) Then
Result = Result & CharDelimiter & Codes(CurrentMatch - 1)
Else
Result = Result & CharDelimiter & NotFoundReplacement
End If
Next n
' Remove leading Char Delimiter.
Result = Right(Result, Len(Result) - Len(CharDelimiter))
' Remove Char Delimiter following a Word Delimiter.
getMorseCode = Replace(Result, WordDelimiter & CharDelimiter, WordDelimiter)
End Function
Sub TESTgetMorseCode()
MsgBox getMorseCode("""The character '%' cannot be found!""")
'Debug.Print getMorseCode("""The character '%' cannot be found!""")
End Sub

VBasic2008
- 44,888
- 5
- 17
- 28
-
Thank you so much brother, it works flawlessly, I've got one more question, may you please just rotate script for converting from Morse to English (I'm real bad in VB but I need this haha) . Thanks! <3 – Saša Bugarin Dec 20 '20 at 23:11
-
This only returns FALSE for me.. The Function Arguments window shows the correct result, but the cell only displays FALSE. I've enabled macros and searched for other causes. What else could I do? Thanks – rgfuller Oct 14 '22 at 02:21