0
使用Excel宏(VBA),我需要为一系列字符串生成哈希总数。我目前的功能不能输出正确的结果。VBA生成自定义哈希总数
如何导出散列总是这样:
减去每个接收帐户的 第一个11个字符始发帐户的前11个字符。
- 如果帐号少于11个字符,请在右侧填写
0
。 - 如果 帐号有一个字母,请将字母转换为
0
。 - 导出每个接收帐户的值。取绝对值,即 ,忽略负号。
- 如果帐号少于11个字符,请在右侧填写
添加绝对值
拿结果的前11字符。如果结果小于 比11个字符,请在左侧填写
0
。
这是我现在有:
Function cleanString(text As String) As String
Dim output As String
Dim i As Integer
Dim c 'since char type does not exist in vba, we have to use variant type.
For i = 1 To Len(text)
'With Sheet2
MsgBox (i)
c = Mid(text, i, 1) 'Select the character at the i position
If (c >= "a" And c <= "z") Or (c >= "A" And c <= "Z") Then
output = output & "0" 'add the character to your output.
Else
output = output & c 'add the replacement character (space) to your output
End If
'End With
Next
cleanString = output
End Function
Function generateHash(LastRows As Long) As Double
Dim output As Double
'Dim c 'since char type does not exist in vba, we have to use variant type.
Dim Orig As String
Dim AccNo As String
Dim temp As Long
Orig = Left(Range("B3") & String(34, " "), 34)
For LastRows = 9 To LastRows
With Sheet2
AccNo = Left(.Cells(LastRows, 5) & String(11, " "), 11)
AccNo = cleanString(AccNo)
temp = Abs(AccNo - Orig)
output = output + temp
'MsgBox (output)
End With
Next
generateHash = output
End Function
这些都是26个样本字符串,这应该产生的散列总的 “31341437052”
0039002572
0039002580
0030015769
0030016412
0259001090
0259001111
0039002637
0100703387
0100703395
0100703425
0100703433
0100703441
0100703450
0100703468
0100703476
0100703484
0011227958
0011228946
951382892
951700711
301402570
402705981
0030001620
0036001622
111111111
222222222
在如何是导致错误的代码?在哪一行错误? –
没有错误。输出散列总数必须等于31341437052与26个样本字符串。它现在显示了一些其他数字。 –