Hash and Square Brackets

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Hash and Square Brackets

Post by Rudi »

Hi,

What are the purposes of the hash and square brackets?

Example:

Code: Select all

#If Win64 Then
Public Declare PtrSafe Function GetTickCount Lib "Kernel32" () As Long
#Else
Public Declare Function GetTickCount Lib "Kernel32" () As Long
#End If

Code: Select all

[selRow] = Target.Row
[selCol] = Target.Column
I have seen these before but never bothered about them until seeing a few recent examples that use them. They have now triggered my interest to get a decent explanation for their purpose. I remember something about the square brackets being a shortcut version of a range or something???
TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Hash and Square Brackets

Post by HansV »

#If, #Else and #End If are compiler directives. They are used to make VBA look at code only if some condition is met.

In your example, if Win64 is True (or non-zero), VBA will "see" the first declaration, otherwise it will "see" the second one. This is useful because the first declaration will not be recognized on older versions.

In Excel VBA, the square brackets [ ] are a shortcut for the Evaluate function. For example, [2+3] is equivalent to Evaluate("2+3") and [A1] is equivalent to Evaluate("A1").
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Hash and Square Brackets

Post by HansV »

Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Hash and Square Brackets

Post by Rudi »

1. Is this similar to the hash in this line: Open T For Input As #1

2. If I use the line: [B10].Select, Range("B10") gets selected. So this can also be used as a range reference?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Hash and Square Brackets

Post by Rudi »

TX for those links. I'll definitely review these... TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Hash and Square Brackets

Post by HansV »

1) No, that's a completely different use of #. The use of #1 as file number in file operations such as Open, Line Input, Print, Write and Close has nothing to do with conditional compilation of code using #Const, #If etc.

2) Yes, [B1] is a short way of referring to cell B1 on the active sheet where you would ordinarily use Range("B1"), and [OtherSheet!D37] is a short way of referring to cell D37 on OtherSheet, where you would ordinarily use Worksheets("OtherSheet").Range("D37").
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Hash and Square Brackets

Post by Rudi »

Understood. TX.

I could see myself using the square brackets as range reference shortcuts here and there, but the hash code is out of my league as I doubt I'll ever need to create code for different platforms. :whisper: (At least I don't think so!)

This statement sums up the # code:
The behavior of the #If...Then...#Else directive is the same as the If...Then...Else statement, except that there is no single-line form of the #If, #Else, #ElseIf, and #End If directives; that is, no other code can appear on the same line as any of the directives. Conditional compilation is typically used to compile the same program for different platforms.
TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.