2017-05-03 15 views
0

Excelマクロ(VBA)では、一連の文字列のハッシュ合計を生成する必要があります。現在の関数は正しい結果を出力できません。ハッシュ合計を導出する方法カスタムハッシュ合計を生成するVBA

のようなある:

  1. は、各受信アカウントの 最初の11個の文字からアカウントを起点の最初の11個の文字を引きます。

    • アカウント番号が11文字未満の場合は、右側に0と入力します。
    • アカウント番号にアルファベットが含まれている場合は、アルファベットを0に変換してください。
    • 各受信アカウントの値を取得します。 の絶対値をとって、負の符号を無視してください。
  2. 絶対値

  3. を追加した結果の最初の11文字を取ります。結果が1135文字未満の場合は、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 

これらは "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

エラーはありません。出力されたハッシュトータルは、26個のサンプルストリングと31341437052に等しくなければなりません。それは今、他の数字を示しています。 –

答えて

0

OKのハッシュ合計を生成しなければならない26個のサンプル文字列、です。だから私はそれが欠けていたいくつかのパラメータだったと思う。

これを「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 
関連する問題