Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Difference between & and +

What is the difference between:

string1 + string2

and

string1 & string2

Are they equivalent? Why have two different symbols that do the same thing?

like image 771
Alex Gordon Avatar asked Jul 29 '10 17:07

Alex Gordon


People also ask

What is difference between VBA and VB?

VBA stands for Visual Basic For Applications and its a Visual Basic implementation intended to be used in the Office Suite. The difference between them is that VBA is embedded inside Office documents (its an Office feature). VB is the ide/language for developing applications.

What is difference between VBA and macros?

VBA is a programming language for developing automated tasks for Excel and other Microsoft Office programs like Word and PowerPoint while macros is a collection of commands that is used to replace a repetitive series of keyboard and mouse actions in an application such as MS Excel.

Does != Work in VBA?

Bookmark this question. Show activity on this post. The problem is that != does not work as a function in excel vba.

What is better than VBA?

Python code is reproducible and compatible, which makes it suitable for further manipulation by other contributors who are running independent projects. Unlike the VBA language used in Excel, data analysis using Python is cleaner and provides better version control.


2 Answers

The expressions are the same as long as the operands are strings; if not, + might add them instead depending on type conversions. & guarantees you won't get anything except a string concatenation, and will convert operands to strings if possible to do so.

There's an MSDN entry about Concatenation operations in Visual Basic that explains it:

The & Operator (Visual Basic) is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.

like image 166
Michael Mrozek Avatar answered Oct 06 '22 07:10

Michael Mrozek


The two expressions are equivalent, but the operators are not. + can be used as an arithmetic operator as well as for string concatenation, & can only be used for the latter.

like image 27
Georg Fritzsche Avatar answered Oct 06 '22 06:10

Georg Fritzsche