vba #if、#else、#end if 中的哈希符号是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6325486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:34:59  来源:igfitidea点击:

What do the hash signs in #if, #else, #end if mean?

excelvbaconditional-statements

提问by Splatgore

I'm writing code to check 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 in use.

我正在编写代码来检查文件是否可以从 SharePoint 中签出,如果不是,则提醒用户并告诉他们该文件正在被其他人使用以及谁在使用该文件。

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

我在http://www.xcelfiles.com/IsFileOpen.html#anchor_37 上遇到了一段代码

The code seems to work in test scenarios so I am planning to adapt it for my purposes. I'm having trouble understanding some of the syntax.

该代码似乎适用于测试场景,因此我计划根据我的目的对其进行调整。我在理解某些语法时遇到问题。

#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 don't get the significance of the '#' symbol.

我明白代码的作用,但我不明白“#”符号的意义。

Another example:

另一个例子:

hdlFile = FreeFile

Open strPath For Binary As #hdlFile

strXl = Space(LOF(hdlFile))

Get 1, , strXl

Close #hdlFile

It is a pain to google because it is so vague.

谷歌很痛苦,因为它太模糊了。

回答by Chris Hutchinson

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.

散列符号表示预处理器命令,它们是在编译之前处理的命令,本质上产生动态/条件代码。这些类型的命令通常在 C/C++ 等语言中用于管理跨平台编程技术。一个常见的用法是检查特定环境或平台(即 VBA、Windows、MacOSX 等),然后实现特定于平台的代码。

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

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

回答by Joshua Evensen

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

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

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

哎呀,这是为 vb.net 是不是。我认为相同的概念。

回答by Eric Nolan

The accepted answer is correct that your first code example are compile directives.

接受的答案是正确的,您的第一个代码示例是编译指令。

It's worth pointing out in case someone else comes across this question that your second code example is a different thing. In this case these are file numbers used to identify text streams. You will see them used in the context of the commands below. They are completely unrelated to compiler directives.

值得指出的是,如果其他人遇到这个问题,您的第二个代码示例是另一回事。在这种情况下,这些是用于标识文本流的文件编号。您将在以下命令的上下文中看到它们的使用。它们与编译器指令完全无关。

Open "c:\test.txt" For Input As #hdlFile Input #hdlfile Print #hdlfile Write #hdlfile Close #hdlfile

打开 "c:\test.txt" For Input As #hdlFile Input #hdlfile Print #hdlfile Write #hdlfile Close #hdlfile