VBA supports two ways of programming.
The first is typeless programming. Easy to use, compact, very suited to small macros. This is also the syntax that VBScript chose to implement from VBA.
Note Dim A
implies Dim A as Variant
.
The second is programming efficiently with typed programming, ie programming to the CPU like C does.
In typeless everything is an abstract datatype that requires processing to prepare it every time you use it. It needs to be a specific type when the CPU uses it.
When typed is requires no processing when used as that type. Remembering that Basic auto converts types in both cases if needed known as Let
coercion (https://learn.microsoft.com/en-us/openspecs/microsoft_general_purpose_programming_languages/ms-vbal/74614d3e-7068-4c33-b149-029534522472). So you have to declare it correctly for speed as Basic fixes slow declares.
This is a Variant in memory. Note most fields are one from the union.
typedef struct tagVARIANT {
union {
struct {
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union {
LONGLONG llVal;
LONG lVal;
BYTE bVal;
SHORT iVal;
FLOAT fltVal;
DOUBLE dblVal;
VARIANT_BOOL boolVal;
VARIANT_BOOL __OBSOLETE__VARIANT_BOOL;
SCODE scode;
CY cyVal;
DATE date;
BSTR bstrVal;
IUnknown *punkVal;
IDispatch *pdispVal;
SAFEARRAY *parray;
BYTE *pbVal;
SHORT *piVal;
LONG *plVal;
LONGLONG *pllVal;
FLOAT *pfltVal;
DOUBLE *pdblVal;
VARIANT_BOOL *pboolVal;
VARIANT_BOOL *__OBSOLETE__VARIANT_PBOOL;
SCODE *pscode;
CY *pcyVal;
DATE *pdate;
BSTR *pbstrVal;
IUnknown **ppunkVal;
IDispatch **ppdispVal;
SAFEARRAY **pparray;
VARIANT *pvarVal;
PVOID byref;
CHAR cVal;
USHORT uiVal;
ULONG ulVal;
ULONGLONG ullVal;
INT intVal;
UINT uintVal;
DECIMAL *pdecVal;
CHAR *pcVal;
USHORT *puiVal;
ULONG *pulVal;
ULONGLONG *pullVal;
INT *pintVal;
UINT *puintVal;
struct {
PVOID pvRecord;
IRecordInfo *pRecInfo;
} __VARIANT_NAME_4;
} __VARIANT_NAME_3;
} __VARIANT_NAME_2;
DECIMAL decVal;
} __VARIANT_NAME_1;
} VARIANT;
https://learn.microsoft.com/en-us/windows/win32/api/oaidl/ns-oaidl-variant
This is a VBA string (known as a BStr)
A BSTR is a composite data type that consists of a length prefix, a
data string, and a terminator. The following table describes these
components.
Item Description
Length prefix A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator.
Data string A string of Unicode characters. May contain multiple embedded null characters.
Terminator A NULL (0x0000) WCHAR.
Previously, some versions of Mac operating systems defined this data
type in a different way, and some Microsoft code running on Mac
computers used this data type. This documentation no longer describes
these obsolete details.
A BSTR is a pointer. The pointer points to the first character of the
data string, not to the length prefix.
BSTRs are allocated using COM memory allocation functions, so they can
be returned from methods without concern for memory allocation.
https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
Option Explicit
or not. It seems to be about the declaration of the variableMsg
itself. Does one really needs this variable anf if yes what is it good for? But at the end this is possibly opinion based because I would probably use constants likeConst MORNING = "Good Morning"
etc.