Home » excel » excel – Run-Time Error 6 – How to make Mod handle a double?

excel – Run-Time Error 6 – How to make Mod handle a double?

Posted by: admin March 9, 2020 Leave a comment

Questions:

I get always a runtime error in vba:

 Sub rsa()

Dim c1 As Long
Dim c2 As Long
Dim z As Long
Dim e As Long

pt = "xa"
n = 187
e = 7

For i = 1 To Len(pt)

    b = Mid$(pt, i, 1)

        If b <> " " Then

             z = Asc(UCase(b))
             'Here is the problem:
             c = z ^ e Mod n
             Text = Text & c

        Else

        Text = Text & " "

        End If

Next i
Cells(20, 4).Value = Text
End Sub

I get the runtime error at c = z ^ e Mod n.

I tried different datatypes, but without a solution.

How to&Answers:

A Mod B is equals to A - Int(A / B) * B, so you can try to use c = z ^ e - Int(z ^ e / n) * n instead c = z ^ e Mod n. It works for me

Answer:

Runtime error 6 is a numeric overflow operation. One of your variables is not defined big enough or without enough precision. You need to show more examples etc… for people to give you a better idea. First thing I would do is print off ALL the numbers BEFORE you execute the formula.

Use the datatype Double and when converting to Double use CDbl()