Microsoft KB Archive/213666

= XL2000: Only One Character Is Returned by a Declared Function =

Article ID: 213666

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213666





SYMPTOMS
In Microsoft Excel 2000, if you use a formula in a worksheet to call a function that is declared in a Microsoft Visual Basic module, there may be only one character returned by the function.



CAUSE
This behavior occurs because the function returns a UNICODE string, whereas Excel is expecting an American National Standards Institute (ANSI) string. UNICODE is a double-byte character set in which the second byte is reserved in memory. UNICODE appears to ANSI functions as a null byte and is assumed to be the string terminator. This discrepancy results in the string being truncated after the first character.



WORKAROUND
To work around this issue, create a user-defined wrapper function to call the declared function.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Example
You can create a user-defined wrapper function to call the declared function. The MessageBox function and the declaration are used in the following example:  Open a new workbook in Microsoft Excel and start the Visual Basic Editor (press ALT+F11). On the Insert menu, click Module.  Type or paste the following code in the module sheet: Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _ (ByVal hwnd As Long, ByVal lpText As String, _  ByVal lpCaption As String, ByVal wType As Long) As Long

Function MyMessageBox(hwnd As Long, lpText As String, _  lpCaption As String, wType As Long) As Long MyMessageBox = MessageBox(hwnd, lpText, lpCaption, wType)

End Function This function is a wrapper function that is entered directly into the worksheet.  Switch to Excel (press ALT+F11). In cell A1, type the following formula:

=MyMessageBox(0,"Works fine in Excel 2000","Title Bar",0)

Notice that when you press ENTER, the title bar and message text are complete. Click OK.</li></ol>

<div class="moreinformation_section">

MORE INFORMATION
<div class="references_section">