ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   The use of the "#" sign in VBA code (https://www.excelbanter.com/excel-programming/356506-use-sign-vba-code.html)

ExcelMonkey

The use of the "#" sign in VBA code
 
I just came across some code which uses the "#" sign. I seems to be using it
when delcaring constants:

Const Variable1 As Integer = 10#

What is the # sign doing? Why would you use it?

Thanks

EM


Tom Ogilvy

The use of the "#" sign in VBA code
 
It is used to explicitly cast the type of a variable or constant (in the case
of numbers)

From the immediate window.
? typename(10#)
Double
? typename(10%)
Integer
? typename(10!)
Single
? typename(10&)
Long

In the instance you show, since the constant is declared as integer, it
seems a bit redundant. It would more commonly be used to avoid a VBA's
implicit conversion when it causes problems.

for example
? 32000 * 10
causes an overflow error (error 6) since both variables are treated as
integers

? 32000! * 10
320000
works OK becuase 3200 is then treated as a long

You might be more familiar with variable declarations that use a $ sign for
string

Dim i$, j$

rather than
Dim i as string, j as String

? typename(i$)
String


--
Regards,
Tom Ogilvy


"ExcelMonkey" wrote:

I just came across some code which uses the "#" sign. I seems to be using it
when delcaring constants:

Const Variable1 As Integer = 10#

What is the # sign doing? Why would you use it?

Thanks

EM



All times are GMT +1. The time now is 10:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com