1

我用 VBA 创建了一个 Excel 工作簿来帮助创建用户帐户。它生成 8 个字符的密码,然后以拼音方式显示密码。

一切正常,我只想改进我的代码。

这部分代码采用密码中的字母字符并吐出北约语音等效项。似乎我应该能够以一半的代码量容纳大小写字符,我只是不知道如何。

Public Function Wordify(char As String)
 Wordify = Switch(char = "A", "ALFA", char = "B", "BRAVO", char = "C", "CHARLIE", char = "D", "DELTA", char = "E", "ECHO", char = "F", "FOXTROT", _
  char = "G", "GOLF", char = "H", "HOTEL", char = "I", "INDIA", char = "J", "JULIETT", char = "K", "KILO", char = "L", "LIMA", char = "M", "MIKE", _
  char = "N", "NOVEMBER", char = "O", "OSCAR", char = "P", "PAPA", char = "Q", "QUEBEC", char = "R", "ROMEO", char = "S", "SIERRA", char = "T", "TANGO", _
  char = "U", "UNIFORM", char = "V", "VICTOR", char = "W", "WHISKEY", char = "X", "XRAY", char = "Y", "YANKEE", char = "Z", "ZULU", _
  char = "a", "alfa", char = "b", "bravo", char = "c", "charlie", char = "d", "delta", char = "e", "echo", char = "f", "foxtrot", _
  char = "g", "golf", char = "h", "hotel", char = "i", "india", char = "j", "juliett", char = "k", "kilo", char = "l", "lima", char = "m", "mike", _
  char = "n", "november", char = "o", "oscar", char = "p", "papa", char = "q", "quebec", char = "r", "romeo", char = "s", "sierra", char = "t", "tango", _
  char = "u", "uniform", char = "v", "victor", char = "w", "whiskey", char = "x", "xray", char = "y", "yankee", char = "z", "zulu")
End Function
4

2 回答 2

4

进一步简化 Ian 的代码:


Public Function Wordify(char As String) As String
  On Error Resume Next  
  Wordify = Split("alpha,bravo,charlie,delta,echo,foxtrot,golf,hotel,india,juliett,kilo,lima,mike,november,oscar,papa,quebec,romeo,sierra,tango,uniform,victor,whiskey,xray,yankee,zulu", ",")(Asc(LCase(char)) - 97)
End Function

PS。我还将“alfa”替换为北约拼音字母表中使用的拼写;“α”。

于 2013-07-28T07:39:03.817 回答
2

试试下面的代码。简化版。

Public Function Wordify(char As String)
Phonetics = Split("alfa,bravo,charlie,delta,echo,foxtrot,golf,hotel,india,juliett,kilo,lima,mike,november,oscar,papa,quebec,romeo,sierra,tango,uniform,victor,whiskey,xray,yankee,zulu", ",") 'storing your custom values into an array Phonetics
AsciofChar = Asc(char) 'finding the ascii of the character passed to the function
If AsciofChar >= 65 And AsciofChar <= 90 Then 'checking whether the char is upper case
    Wordify = UCase(Phonetics(AsciofChar - 65)) 'selecting the value from the array corresponding to the char passed
ElseIf AsciofChar >= 97 And AsciofChar <= 122 Then 'checking whether the char is in lower case
    Wordify = LCase(Phonetics(AsciofChar - 97)) 'selecting the value from the array corresponding to the char passed
End If
End Function
于 2013-07-28T07:19:36.067 回答