在制作Visual Basic Excel宏以将摩尔斯电码转换为英语以及将摩尔斯电码转换为英语时,是否可以获得帮助
我需要帮助,使VB Excel宏,将采取从InputBox输入,并将其从英语转换为莫尔斯,反之亦然,然后显示在MessageBox的结果。我被困住了,我不知道怎么才能成功。提前感谢您的帮助请参见以下内容(仅供参考-您可能需要将代码转换为VBA,但我相信您可以做到) 英语对摩尔斯电码在制作Visual Basic Excel宏以将摩尔斯电码转换为英语以及将摩尔斯电码转换为英语时,是否可以获得帮助,excel,vba,decode,converters,morse-code,Excel,Vba,Decode,Converters,Morse Code,我需要帮助,使VB Excel宏,将采取从InputBox输入,并将其从英语转换为莫尔斯,反之亦然,然后显示在MessageBox的结果。我被困住了,我不知道怎么才能成功。提前感谢您的帮助请参见以下内容(仅供参考-您可能需要将代码转换为VBA,但我相信您可以做到) 英语对摩尔斯电码 虽然这几乎是默认情况下用a完成的,但我还是坚持使用数组作为一个有趣的替代方案 代码 Option Explicit Function getMorseCode( _ ByVal s As String,
- 虽然这几乎是默认情况下用a完成的,但我还是坚持使用数组作为一个有趣的替代方案
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
太谢谢你了,我明天早上尽量去不客气。祝你有一个美好的一天谢谢你这么多的兄弟,它的工作完美无瑕,我还有一个问题,请你只是旋转脚本转换从莫尔斯到英语(我在VB中真的很差,但我需要这个哈哈)。谢谢
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