The MsgBox function syntax has these arguments:
Argument Description
prompt Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used. If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return – linefeed character combination (Chr(13) & Chr(10)) between each line.
Think about how data types are determined for the result of an arithmetic operation. The result has to be determined prior to any assignment, and so has its datatype.
In case of literals, VBA will assume the smallest data type appropriate for the literal. Thus a literal '256' would be an integer. An integer 256 multiplied by integer 256 will overflow.
Code:
?TypeName(256)
Integer
To coerce a literal or a constant value into a desired size, you need to provide a type declaration character. This will succeed:
Code:
?256& * 256&
Or you can use conversion functions as previously suggested for the literals. You cannot use it for a constant value since conversion functions aren't themselves a constant. Thus type declaration character is the only sole for cases such as defining a constant to be used by a Windows API function:
Code:
Const SC_MOVE As Long = &HF010&
If you omit the type character, the value is assumed to be an integer and returns -4080 which is not what SC_MOVE is actually defined in the Windows header files. The correct value should be 61456. By coercing it into long, you can get the correct value.
In case of literals, VBA will assume the smallest data type appropriate for the literal. Thus a literal '256' would be an integer. An integer 256 multiplied by integer 256 will overflow.
Code:
?TypeName(256)
Integer
To coerce a literal or a constant value into a desired size, you need to provide a type declaration character. This will succeed:
Code:
?256& * 256&
Or you can use conversion functions as previously suggested for the literals. You cannot use it for a constant value since conversion functions aren't themselves a constant. Thus type declaration character is the only sole for cases such as defining a constant to be used by a Windows API function:
Code:
Const SC_MOVE As Long = &HF010&
If you omit the type character, the value is assumed to be an integer and returns -4080 which is not what SC_MOVE is actually defined in the Windows header files. The correct value should be 61456. By coercing it into long, you can get the correct value.
IME, "Print" has not worked for me before. I always had to say "Debug.Print" inside procedures. I assume you copied "?256& * 256&" and pasted it into a procedure instead of into Immediate windows. VBA editor changes it to the code that you displayed which then breaks it until you manually add in "Debug." preceding the "Print".
The first two code snippets were tested in Immediate Window, which is why they had the "?" prefixed, rather than "Debug.Print".
I have always thought it was a good policy to rein in haphazard positing of assumptions, especially when they are unwarranted. On the other hand, it can be fun, I suppose.
IME, "Print" has not worked for me before. I always had to say "Debug.Print" inside procedures. I assume you copied "?256& * 256&" and pasted it into a procedure instead of into Immediate windows. VBA editor changes it to the code that you displayed which then breaks it until you manually add in "Debug." preceding the "Print".
The first two code snippets were tested in Immediate Window, which is why they had the "?" prefixed, rather than "Debug.Print".
Hope I did not scare you away. I wonder if your email address is current. Not to worry. If I don't hear from you soon I will post something in UA as a prompt.