VBA 左移和右移

'左移函数

Function LShift(Value, Shift)

    Set sc = CreateObject("MSScriptControl.ScriptControl")

    sc.Language = "JScript"

    LShift = sc.Eval(Value & "<<" & Shift)

End Function

 

'右移函数

 

Function RShift(Value, Shift)

    Set sc = CreateObject("MSScriptControl.ScriptControl")

    sc.Language = "JScript"

    RShift = sc.Eval(Value & ">>" & Shift)

End Function

 

工作中遇到的加密算法示例:

Sub MyModelID()

 

    Dim i As Integer

    Dim rs  As Long

    Dim n_brandid  As Long

    Dim n_modelid  As Long

   

    For i = 2 To UsedRange.Rows.Count

        rs = Cells(i, 2) Mod LShift(1, 7) 'Brandid

        n_brandid = rs + LShift(rs, 7)

        n_modelid = Cells(i, 1) Xor n_brandid

        n_modelid = LShift(n_modelid, 7) + rs

        Cells(i, 6) = n_modelid

       

        If IsNumeric(Cells(i, 3)) And IsEmpty(Cells(i, 3)) <> True Then

            Cells(i, 8) = Cells(i, 3) * 2 / 60 + 2

            Cells(i, 9) = Cells(i, 4) / 60

            Cells(i, 10) = Cells(i, 5) / 60

        Else

            Cells(i, 8) = 8888

            Cells(i, 9) = 8888

            Cells(i, 10) = 8888

        End If

           

       

       ' Debug.Print Cells(i, 6)

    Next

   

    MsgBox "完成!"

   

End Sub

 

posted @ 2021-05-13 08:32  devilan  阅读(886)  评论(0编辑  收藏  举报