2015-09-19 120 views
7

我试图让I/O如下:转换数字字符字母字符

输入:123490
输出:BCDEJA

逻辑很简单:

如果
strarr(i)=0,1,2,3,4,5,6,7,8,9
then
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 

谁能帮我在哪里,我错了吗?

+1

每个字母都是'Chr( + 65)'。 – Jeeped

回答

10

利用的开始ASCII字符编号(...?)并通过digi进行调整你正在转换。大写字母A是ASCII 0×41或65十进制。

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) 

Numbers to Characters

3

由于Jeeped说你可以使用Chr函数将一个数字转换为一个字母,使用ASCII。

在另一方面,与可以有多个值的单一变量的工作,而不是使用这么多if的我会建议使用case select模型时,使用strarr(i)作为控制器,将简化您的代码,并会是一个更多可读性。另外,我不会将不同的值写入单元格,而是使用一个临时变量来存储聚合值,对于您来说不那么麻烦,并且稍微快一点,因为您不会读取/写入表单,重新只是在后台

2

工作这应该让你开始:

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

请注意,“65'stands资本A中值,小写字母从'97

+0

应该使用65而不是64作为显式值,对于他来说A是0,所以使用64将会使-1减去标记。 –

+0

谢谢你,以为它从1开始 –

2
=CHAR(64 + 1) 
will Give "A" 
=CHAR(64 + 2) 
will Give "B" 
=CHAR(64 + 3) 
will Give "C" 

so on..... 
+1

OP想要他的转换从A = 0开始。你的转换似乎是以1为因子的。 – Jeeped

4

我喜欢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秒。
  • 使用Mid $在LHS把它带到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 
+0

我从来没有想到会有那么显着的性能提升。从现在开始,我将开始在大型变体阵列处理中使用这些方法。 – Jeeped

+0

我知道这是一个很老的问题,但我想发表另一种侧重于性能的方法。包含NumChr结果进行比较可以吗? PS。 NumChr的最后一行有一个错字。 – BrakNicku

相关问题