-1

I have a simple code from a book on VBA that displays a MsgBox with greetings. The code is below:

Sub GreetMe5()
Dim Msg As String
If Time < 0.5 Then Msg = “Morning”
If Time >= 0.5 And Time < 0.75 Then Msg = “Afternoon”
If Time >= 0.75 Then Msg = “Evening”
MsgBox “Good “ & Msg
End Sub

My question is that what's the purpose of declaring the variable 'Msg as String' if it could be done without declaring one? The code without the variable should be like this:

Sub Greetings1()
If Time < 0.5 Then MsgBox "Good Morning"
If Time >= 0.5 And Time < 0.75 Then MsgBox "Good Afternoon"
If Time >= 0.75 Then MsgBox "Good Evening"
End Sub

Thank You

2
  • 2
    You got the UI review back and it said that the message box needs a more descriptive title. Which version did you find easier to modify? Commented Jun 18, 2022 at 16:27
  • 2
    I do not think the question is about using Option Explicit or not. It seems to be about the declaration of the variable Msg 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 like Const MORNING = "Good Morning" etc.
    – Storax
    Commented Jun 18, 2022 at 19:10

3 Answers 3

1

The small example of code you give is in itself quite poor quality as it is what you would expect a naive user of VBA to come up with. Letts say this is part of a programm you will distribute to overseas offices in your company. The greeting should of course be in the local language. DO you want to write 50 versions of the code you propose, 1 for each language used, or would something more advanced be better.


Public Enum Greeting

    NoGreeting = 0
    Morning 
    Afternoon
    Evening
    ' other greeting message references are added here
End Enum


Sub GreetUser()
Dim Msg As Greeting  ' where greeting is the name of an enumeration
If Time < 0.5 Then Msg = Greeting.Morning
If Time >= 0.5 And Time < 0.75 Then Msg = Greeting.Afternoon
If Time >= 0.75 Then Msg = Greeting.Evening
MsgBox Messages(getsystemLocale).Greetings(msg)
End Sub

1
  • Nice, voted up! But I think it is good to also show how that Enumeration should look. Otherwise, people less skilled will not understand what you mean and your (good) answer will be voted down...
    – FaneDuru
    Commented Jun 19, 2022 at 14:50
1

Declaring variables with datatype allocates memory beforehand.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 11 at 4:58
-2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.