XL2000: Only One Character Is Returned by a Declared Function (213666)



The information in this article applies to:

  • Microsoft Excel 2000

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: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

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:
  1. Open a new workbook in Microsoft Excel and start the Visual Basic Editor (press ALT+F11).
  2. On the Insert menu, click Module.
  3. 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.
  4. Switch to Excel (press ALT+F11).
  5. 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.
  6. Click OK.

MORE INFORMATION

REFERENCES

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbnofix kbprb kbProgramming KB213666