VBA生成自定义哈希总计

时间:2017-05-03 14:25:24

标签: excel-vba hash vba excel

使用excel宏(VBA),我需要为一系列字符串生成哈希总计。我当前的功能无法输出正确的结果。

如何导出哈希总数是这样的:

  1. 从中减去原始帐户的前11个字符 每个接收帐户的前11个字符。

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

  3. 取结果的前11个字符。如果结果较少 如果不是11个字符,请在左侧填充0

  4. 这就是我现在所拥有的:

    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 个答案:

没有答案