vba Excel VBA中:=和=的区别是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40768172/
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
What is the difference between := and = in Excel VBA
提问by Moreno
I have been working with Excel for a while, yet i have never read what is the difference between these two operators ("regardless of i have used both")
:=
and =
in Excel VBA
我已经使用 Excel 有一段时间了,但我从来没有读过这两个运算符(“不管我都使用过”):=
和=
Excel VBA之间的区别是什么
回答by Martin Dreher
As you already know, =
is used to assign values or set objects - e.g. i=1
如您所知,=
用于分配值或设置对象 - 例如i=1
:=
on the other hand (like Comintern mentioned), is used to to assign a value to a certain namedargument, afaik only ever inside a method or function.
:=
另一方面(如 Comintern 提到的),用于为某个命名参数分配一个值,afaik 只在方法或函数内部。
Consider the following example: you could use something like MsgBox "Hello World", , "Title1"
- specifying MsgBox
's arguments in the default order - the prompt
, the default Buttons
-style, then the Title
.
考虑以下示例:您可以使用类似的东西MsgBox "Hello World", , "Title1"
-MsgBox
按默认顺序指定的参数 - prompt
、默认Buttons
样式,然后是Title
.
Alternatively, one could use :=
to write MsgBox Title:="Title1", prompt:="Hello world"
或者,可以:=
用来写MsgBox Title:="Title1", prompt:="Hello world"
Notice that
请注意
the order of the arguments is of no importance here and
there is no need to specify empty placeholders for default-arguments
, ,
.
参数的顺序在这里并不重要,并且
无需为 default-arguments 指定空占位符
, ,
。
回答by Chrismas007
Let us take for example the Range.Find
method
让我们以方法为例Range.Find
expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
表达。查找(什么,之后,LookIn,LookAt,SearchOrder,SearchDirection,MatchCase,MatchByte,SearchFormat)
That is a LOTof conditions to set! But you just want a simple search of the number 2
in Range("A1:A500")
:
这是要设置的很多条件!但是,你只是想简单的搜索次数2
在Range("A1:A500")
:
Without the :=
operator, you would have to use commas to get to any optional variables to set:
如果没有:=
运算符,您将不得不使用逗号来获取要设置的任何可选变量:
Range("A1:A500").Find(2, , xlValue, , , , , , )
With the :=
operator, you can specify which conditions you want without delineating through all the default settings:
使用:=
运算符,您可以指定所需的条件,而无需描述所有默认设置:
Range("A1:A500").Find(what:=2, lookin:=xlValues)