这种题目用递归做好些. 代码多在处理0的不同情况. 这里0在1或2后做字母处理, 单独时就做0处理. Dim arr(1 To 26) As String Private Sub CommandButton1_Click() Dim tmpStr As String Dim tmp As String For i = 65 To 90 arr(i - 64) = Chr(i) Next i tmpStr = "" tmp = Cells(1, 1) Range(Cells(2, 1), Cells(10000, 1)).Clear Cells(1, 1).Select tm = Timer Call GetAlpha(tmp, tmpStr) MsgBox "Processed " & Format((Timer - tm), "0.00000") & " seconds" End Sub Sub GetAlpha(strInput As String, tmpStr As String) Dim tmp As String If Len(strInput) = 1 Then ActiveCell.Offset(1, 0).Select If strInput = "0" Then ActiveCell.Value = tmpStr & "0" Else ActiveCell.Value = tmpStr & arr(strInput) End If ElseIf Len(strInput) = 2 Then ActiveCell.Offset(1, 0).Select If strInput > 26 Then If Right(strInput, 1) = "0" Then tmp = tmpStr & arr(Left(strInput, 1)) & "0" Else tmp = tmpStr & arr(Left(strInput, 1)) & arr(Right(strInput, 1)) End If ActiveCell.Value = tmp ElseIf strInput = "10" Or strInput = "20" Then ActiveCell.Value = tmpStr & arr(strInput) ElseIf strInput = "00" Then ActiveCell.Value = tmpStr & "00" ElseIf Left(strInput, 1) = "0" Then ActiveCell.Value = tmpStr & "0" & arr(Right(strInput, 1)) Else ActiveCell.Value = tmpStr & arr(Left(strInput, 1)) & arr(Right(strInput, 1)) ActiveCell.Offset(1, 0).Select ActiveCell.Value = tmpStr & arr(strInput) End If Else If Left(strInput, 1) <> "0" Then tmp = tmpStr & arr(Left(strInput, 1)) Call GetAlpha(Mid(strInput, 2), tmp) If Left(strInput, 2) <= 26 Then tmp = tmpStr & arr(Left(strInput, 2)) Call GetAlpha(Mid(strInput, 3), tmp) End If Else tmp = tmpStr & "0" Call GetAlpha(Mid(strInput, 2), tmp) End If End If End Sub 基本正确,速度很快,我个人也比较喜欢递归。
[此贴子已经被northwolves于2006-12-31 2:44:58编辑过] |