Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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.

like image 710
yab86 Avatar asked Jan 05 '14 17:01

yab86


People also ask

How do I fix runtime error 6?

The primary way to resolve these problems manually is to replace the Microsoft Corporation file with a fresh copy. Additionally, some Overflow errors can be due to incorrect registry references, so we recommend conducting a registry scan to clean up any invalid entries.

How do I fix runtime error 6 overflow in Excel?

The data type Byte can hold values from 0 to 255. So it causes an error. To fix the error, we either change the data type or reduce the value assigned to the variable “Number.”

How do you fix an overflow error?

To correct this error Make sure that results of assignments, calculations, and data type conversions are not too large to be represented within the range of variables allowed for that type of value, and assign the value to a variable of a type that can hold a larger range of values, if necessary.

Is overflow a runtime error?

A stack overflow is a runtime error that happens when a program runs out of memory in the call stack. The stack overflow generally signals a problem in resource provisioning and has to be fixed in order to allow the program to run and use memory properly.


2 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

like image 87
Dmitry Pavliv Avatar answered Oct 27 '22 05:10

Dmitry Pavliv


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()

like image 21
T McKeown Avatar answered Oct 27 '22 06:10

T McKeown