2017-05-03 115 views
0

使用Excel宏(VBA),我需要为一系列字符串生成哈希总数。我目前的功能不能输出正确的结果。VBA生成自定义哈希总数

如何导出散列总是这样:

  1. 减去每个接收帐户的 第一个11个字符始发帐户的前11个字符。

    • 如果帐号少于11个字符,请在右侧填写0
    • 如果 帐号有一个字母,请将字母转换为0
    • 导出每个接收帐户的值。取绝对值,即 ,忽略负号。
  2. 添加绝对值

  3. 拿结果的前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 
+0

在如何是导致错误的代码?在哪一行错误? –

+0

没有错误。输出散列总数必须等于31341437052与26个样本字符串。它现在显示了一些其他数字。 –

回答

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 
'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