简体   繁体   English

Excel VBA中的=和=之间有什么区别?

[英]What is the difference between := and = in Excel VBA

我一直在使用Excel进行了一段时间,但我从来没有读过什么是这两个运营商之间的差异(“不管我都用了”) :==在Excel VBA

As you already know, = is used to assign values or set objects - eg i=1 如您所知, =用于分配值或设置对象 - 例如i=1

:= on the other hand (like Comintern mentioned), is used to to assign a value to a certain named argument, 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 style,然后是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指定空占位符, ,

Let us take for example the Range.Find method 我们以Range.Find方法为例

expression . 表达 Find( What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat ) 查找( What,After,LookIn,LookAt,SearchOrder,SearchDirection,MatchCase,MatchByte,SearchFormat

That is a LOT of conditions to set! 这是很多条件! But you just want a simple search of the number 2 in Range("A1:A500") : 但是你只想在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM