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

 

上一篇:Aspose功能演示:使用Java以编程方式在 Excel 文件中添加或修改 VBA 宏


下一篇:第一个excel VBA demo —— 添加信号并生成一段Verilog代码