【问题标题】:Convert numeric characters to alphabetic characters将数字字符转换为字母字符
【发布时间】:2015-09-19 10:36:05
【问题描述】:

我正在尝试按如下方式获取 I/O:

输入:123490
输出:BCDEJA

逻辑很简单:

如果
strarr(i)=0,1,2,3,4,5,6,7,8,9
那么
strarr(i) should be = A,B,C,D,E,F,G,H,I,J

代码

str = .Cells(18, "B").Value
strarr() = Split(str) 
For i = LBound(strarr) To UBound(strarr)
  If strarr(i) = 0 Then
  .Cells(24, "B") = "A" & .Cells(24, "B")
  Else
  If strarr(i) = 1 Then
  .Cells(24, "C") = "B" & .Cells(24, "C")
  Else
  If strarr(i) = 2 Then
  .Cells(24, "C") = "C" & .Cells(24, "C")
  Else
  If strarr(i) = 3 Then
  .Cells(24, "D") = "D" & .Cells(24, "D")
  Else
  .
  .
  .

  If strarr(i) = 9 Then
  .Cells(24, "J") = "J" & .Cells(24, "J")
  Else

  End If x10 times
Next i

.Cells(24, "B") = .Cells(24, "B") & .Cells(24, "C") & .Cells(24, "D") & .Cells(24, "E") & .Cells(24, "F") & .Cells(24, "G") & .Cells(24, "H") & .Cells(24, "I") & .Cells(24, "I") & .Cells(24, "J")

.Cells(18, "D").Value = .Cells(24, "B")

Worksheets("Functions").Rows(24).ClearContents
End With

谁能帮我看看我哪里错了?

【问题讨论】:

  • 每个字母都是Chr(<digit> + 65)

标签: vba function excel


【解决方案1】:

利用 ASCII 字符数字 (...?) 并根据您要转换的数字调整它们。国会大厦 A 是 ASCII 0×41 或 65 dec.

Function num_alpha(str As String)
    Dim sTMP As String, d As Long

    For d = 1 To Len(str)
        sTMP = sTMP & Chr(65 + Mid(str, d, 1))
    Next d

    num_alpha = sTMP

End Function

像任何本机工作表函数一样使用。在 D18 中,

=num_alpha(B18)

      

【讨论】:

    【解决方案2】:

    我喜欢 Jeeped 的回答。

    下面的版本可以解决这个问题,并进行了一些调整以提高速度:

    • Mid 作为 LHS 运算符(因为 VBA 中的连接速度较慢)
    • 使用Mid$ChrW$

    在我的测试中,运行时间减少了约 40%(请参阅下面的编辑)

     Function NumChr(strIn As String) As String
            Dim strTemp As String
            Dim lngChar As Long
    
            For lngChar = 1 To Len(strIn)
                Mid$(strIn, lngChar, 1) = ChrW$(65 + Mid$(strIn, lngChar, 1))
            Next
            NumChr = strTemp
        End Function
    

    编辑:添加测试

    1. 初始代码 3.21 秒
    2. 第二个代码需要 1.98 秒

    高层对账

    • 在第一个代码中使用 Mid$ 而不是 Mid 将代码从 3.21 秒缩短到 2.77 秒。
    • 使用 ChrW$ 而不是 Chr 将其从 2.77 秒缩短到 2.43 秒。
    • 在 LHS 上使用 Mid$ 将其缩短到 1.98 秒

    之前的代码

    Function num_alpha(str As String)
        Dim sTMP As String, d As Long
    
        For d = 1 To Len(str)
            sTMP = sTMP & Chr(65 + Mid(str, d, 1))
        Next d
    
        num_alpha = sTMP
    
    End Function
    

    新代码

    Function NumChr(strIn As String) As String
        Dim lngChar As Long
    
        For lngChar = 1 To Len(strIn)
            Mid$(strIn, lngChar, 1) = ChrW$(65 + Mid$(strIn, lngChar, 1))
        Next
        NumChr = strIn
    End Function
    

    测试时间

    Sub Main()
    Call Test
    Call Test2
    End Sub
    
    Sub Test()
    Dim dbTimer As Double
    dbTimer = Timer()
    For i = 1 To 1000000
        s = num_alpha("123490")
    Next
    Debug.Print Timer() - dbTimer
    End Sub
    
    Sub Test2()
    Dim dbTimer As Double
    dbTimer = Timer()
    For i = 1 To 1000000
        s = NumChr("123490")
    Next
    Debug.Print Timer() - dbTimer
    End Sub
    

    【讨论】:

    • 我从未想过会有如此显着的性能提升。从现在开始,我将开始在大型变体数组处理中使用这些方法。
    • 我知道这是一个很老的问题,但我想发布另一种专注于性能的方法。是否可以包含 NumChr 结果进行比较? PS。 NumChr 最后一行有错别字。
    【解决方案3】:

    正如 Jeeped 所说,您可以使用 Chr 函数将数字转换为字母,使用 ASCII。

    另一方面,当使用可以有多个值的单个变量时,我建议使用case select 模型,而不是使用这么多case select 模型,使用strarr(i) 作为控制器,它将简化您的代码,并且会更易读。

    另外,我不会将不同的值写入单元格,而是使用临时变量来存储聚合值,这样可以减少您的麻烦,而且速度会更快一些,因为您无需读取/写入工作表而是只是在后台工作

    【讨论】:

      【解决方案4】:
      =CHAR(64 + 1)
      will Give "A"
      =CHAR(64 + 2)
      will Give "B" 
      =CHAR(64 + 3)
      will Give "C" 
      
      so on.....
      

      【讨论】:

      • OP 希望他的转换从 A = 0 开始。您的转换似乎偏离了 1 倍。
      【解决方案5】:

      这应该让你开始:

      Public Function ConvertValue(iInput As Integer) As String
          ConvertValue = Chr(65 + iInput)
      End Function
      

      请注意,'65'的值代表大写A,小写字母从'97开始

      【讨论】:

      • 应该使用 65 而不是 64 作为显式值,A 对他来说是 0,所以使用 64 你会偏离标记 -1。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2019-12-01
      相关资源
      最近更新 更多