Excel VBA生成自定义哈希总数
使用excel宏(VBA),我需要为一系列字符串生成哈希总数。我当前的函数无法输出正确的结果 如何推导哈希总数如下所示:Excel VBA生成自定义哈希总数,excel,hash,vba,Excel,Hash,Vba,使用excel宏(VBA),我需要为一系列字符串生成哈希总数。我当前的函数无法输出正确的结果 如何推导哈希总数如下所示: 将原始帐户的前11个字符从 每个接收帐户的前11个字符 如果帐号少于11个字符,请在右侧填充0 如果 帐号有字母表,请将字母表转换为0 导出每个接收帐户的值。拿绝对值来说,就是 是的,忽略负号 将绝对值相加 取结果的前11个字符。如果结果是更少 不超过11个字符,用左侧的0填充 这就是我现在拥有的: Function cleanString(text As String)
- 如果帐号少于11个字符,请在右侧填充
0
- 如果
帐号有字母表,请将字母表转换为
李>0
- 导出每个接收帐户的值。拿绝对值来说,就是 是的,忽略负号李>
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
嗯。所以我发现我缺少的只是一些参数 这会将字符串中的任何字母表转换为“0”
代码是如何导致错误的?哪一行出错?没有错误。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
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
'MsgBox (cleanString)
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 String
Dim lCounter As Long
Dim lLastRow As Long
'Find the last row that contains data
With Sheet2
lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Take the original string and making sure there are only 11 characters
Orig = Left(Range("B3") & "00000000000", 11)
'Starts from row 9
For lCounter = 9 To LastRows
With Sheet2
'Ensuring only 11 characters
AccNo = Left(.Cells(lCounter, 5) & "00000000000", 11)
'Clean the string of any alphabet
AccNo = cleanString(AccNo)
'Take the absolute diff of 2nd and 1st string
temp = Abs(AccNo - Orig)
output = output + temp
'MsgBox (output)
End With
Next
generateHash = output
End Function