PDA

View Full Version : Có bạn nào cần thuật toán về sinh dãy số "A,B...AA,BB" như trong Excell không...



lytamhoana6cntt
23-08-2004, 13:45
Thuật toán sinh này hay phết đấy. Tôi đã từng làm chương trình như excell. Thấy thuật toán này cũng hay. Linkto: http://a6land.tk

phongqtran
27-08-2004, 12:55
Cái này được ko? viết = VB :batman:



Public Function NumberToLetter(ByVal pLng As Long) As String
' Date Created :
' Purpose : convert number to letter (1 -> A, 26-> Z, 27 -> AA, 28 -> AB ...)
' Author :
' Coding Issues :
' Uses :
'
' MODIFICATIONS
' DATE WHO DESCRIPTION
On Error GoTo ErrorHandler
Dim strTemp As String
Dim byteMod As Byte
If (pLng > 0) Then
strTemp = ""
While (pLng > 0) And (Len(strTemp) < 10)
byteMod = (pLng Mod 26)
strTemp = IIf(byteMod = 0, "Z", Chr(64 + (pLng Mod 26))) & strTemp ' 64 = A
pLng = IIf(byteMod = 0, (pLng \ 26) - 1, (pLng \ 26))
Wend
NumberToLetter = strTemp
Else
NumberToLetter = "0000000000"
End If
Exit Function
ErrorHandler:
NumberToLetter = "0000000000"
End Function



Public Function LetterToNumber(ByVal pString As String) As Long
' Date Created :
' Purpose : convert letter to number (A->1, Z-> 26, AA->27, AB->28)
' Author :
' Coding Issues :
' Uses :
'
' MODIFICATIONS
' DATE WHO DESCRIPTION

On Error GoTo ErrorHandler
Dim strTemp As String
Dim lngTemp As Double
Dim i As Byte
If (Len(pString) <= 10) Then
pString = UCase(pString)
lngTemp = 0
For i = 1 To Len(pString)
strTemp = Mid(pString, Len(pString) - i + 1, 1)
lngTemp = lngTemp + (Asc(strTemp) - 64) * (26 ^ (i - 1)) ' 65 = A
Next i
LetterToNumber = lngTemp
Else
LetterToNumber = 0
End If
Exit Function
ErrorHandler:
LetterToNumber = 0
End Function