Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of symbol # (hash) in VBA Macro

Tags:

excel

vba

What is the meaning of the use of the # symbol in Excel VBA?

It is used like this:

 a = b /100#

I don't understand the significance of # after the 100?

like image 254
srinivasan Avatar asked Jun 05 '12 02:06

srinivasan


People also ask

What is the use of symbol?

A symbol is a mark, sign, or word that indicates, signifies, or is understood as representing an idea, object, or relationship. Symbols allow people to go beyond what is known or seen by creating linkages between otherwise very different concepts and experiences.

What are 3 common symbols used today?

Some of the most common symbols are the: Heart symbol. Dove symbol. Raven symbol.


1 Answers

The type-declaration character for Double is the number sign (#). Also called HASH

Other type declaration characters are:

  1. Integer %
  2. Long &
  3. Currency @
  4. Single !
  5. Double #
  6. String $

Don't understand the significance of # here.

It implies that when the expression is evaluated, the number in front of the type declaration character is treated as a specific data type instead of as a Variant.

See this example, which are basically the same.

Sub Sample1()
    Dim a#

    a = 1.2

    Debug.Print a
End Sub

Sub Sample2()
    Dim a As Double

    a = 1.2

    Debug.Print a
End Sub

EDIT

Let me explain it a little more in detail.

Consider this two procedures

Sub Sample1()
    Dim a As Double, b As Integer

    b = 32767
    a = b * 100

    Debug.Print a
End Sub

Sub Sample2()
    Dim a As Double, b As Integer

    b = 32767
    a = b * 100#

    Debug.Print a
End Sub

Question: One of them will fail. Can you guess which one?

Ans: The 1st procedure Sub Sample1() will fail.

Reason:

In Sample2, when you do b * 100# the result of calculation will be of type Double. Since it is within the limits of Double, so the calculation succeeds and the result is assigned to variable a.

Now in Sample1, when you do b * 100 the result of calculation will be of type Integer, since both the operands are of type integer. But the result of calculation exceeds the limits of Integer storage. As a result it will error out.

Hope it helps :)

like image 178
Siddharth Rout Avatar answered Oct 03 '22 19:10

Siddharth Rout