Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I cast a long in double in VBA

Tags:

vba

How can I cast a long to double in VBA ? I have googled here and there, and saw some ugly things such as

dim i as long
i = 100
dim d as double
d = i * 1.00000000001

I haven't tested this, I guess this is working, but my application is really data sensitive and I'd like to actually have a cast with no effect on data... Does that not exist ?

like image 950
BuZz Avatar asked Dec 02 '11 13:12

BuZz


People also ask

How do you use long in VBA?

“Long,” as the name says, it should hold the value of something big. “Long” is a numerical data type in VBA Excel. The long data type in Excel VBA can hold the values from 0 to 2, 147, 483, 647 for positive numbers, and for the negative number it can hold from 0 to -2, 147, 483, 648.

How do I change type in VBA?

CInt function example This example uses the CInt function to convert a value to an Integer. Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346.

What is the difference between long and double in VBA?

The difference between the two are how many digits they can hold. As Single holds 4 bytes of data while As Double can hold 8 bytes. If you want a really, really long floating point number (a number with "point something" at the end) then use As Double, otherwise just use As Single.


2 Answers

You don't need to "cast" anything, this will work fine

dim i as long
dim d as double
i = 100
d = i

(and it took me about 30 seconds to start Excel, press ALT-F11, and test it).

like image 151
Doc Brown Avatar answered Oct 28 '22 21:10

Doc Brown


VBA has a bunch of functions that start with 'C' that are used to explicitly cast between types. See CLng, CDbl, CDate, to name a few.

like image 20
Dick Kusleika Avatar answered Oct 28 '22 20:10

Dick Kusleika