#if, #else, #end if … what do the hash signs mean in VBA?

狂风中的少年 提交于 2019-12-28 05:49:09

问题


I'm writing some code which will check to see if a file is available to be checked out of SharePoint and, if it isn't, alert the user and tell them that the file is in use by someone else and who has it is in use by.

I came across a piece of code at this site: http://www.xcelfiles.com/IsFileOpen.html#anchor_37

The code itself is pretty good and seems to work in test scenarios so I am planning to adapt it for my purposes but I'm having trouble understanding some of the syntax being used because I've never seen the likes of it before.

#If Not VBA6 Then

'// Xl97

For i = j - 1 To 1 Step -1

    If Mid(strXl, i, 1) = Chr(0) Then Exit For

Next

i = i + 1

#Else

'// Xl2000+

i = InStrRev(strXl, strFlag1, j) + Len(strFlag1)

#End If

I understand what the code does but I just don't get what the significance of the '#' symbol is?

Another example of its use is :

hdlFile = FreeFile

Open strPath For Binary As #hdlFile

strXl = Space(LOF(hdlFile))

Get 1, , strXl

Close #hdlFile

I'm sure there is a really obvious answer to this but it is one of things that is a pain in the butt to google because it is so vague. :(

Many thanks,

Splat


回答1:


The hash symbols represent a preprocessor command, which are commands that are processed prior to compilation, essentially producing dynamic / conditional code. These types of commands are often used in languages such as C/C++ to manage cross-platform programming techniques. A common usage is to check for a particular environment or platform (ie. VBA, Windows, MacOSX, etc), and then implement platform-specific code.

http://en.wikipedia.org/wiki/Preprocessor




回答2:


The hash indicates that it's a directive. Used for literally including or excluding code from compilation.

http://msdn.microsoft.com/en-us/library/7ah135z7.aspx

whoops that's for vb.net isn't it. Same concept I think.



来源:https://stackoverflow.com/questions/6325486/if-else-end-if-what-do-the-hash-signs-mean-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!