2015-09-19 117 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

相關問題