简体   繁体   中英

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

:= 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.

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 .

Alternatively, one could use := to write 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 , , .

Let us take for example the Range.Find method

expression . Find( 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") :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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