Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Visual Basic Function to Format Complex Numbers


View products that this article applies to.

Summary

Because Microsoft Excel stores complex numbers as text, complex numbers cannot be formatted like real numbers. This article provides sample code for a user-defined function that applies number formats to complex numbers.

↑ Back to the top


More information

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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

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 Complex numbers are real and imaginary coefficients which combine to form a complex number. They are in the form of x+yi or x+yj where x is the real coefficient and y is the imaginary coefficient. Complex numbers always carry a suffix of the letter i or j.

The function in this article takes three arguments: the number to be formatted, a format code for the real component, and a format code for the imaginary component. For example, the function
   =FormatComplex(A1,"0.00","0.0000")
				
would display the complex number in cell A1 with two decimal places for the real component and four decimal places for the imaginary component.

You can also use multisection formats to format positive, negative, and zero components differently. For example, the function
   =FormatComplex(A1,"0.00;-0.00;0","0.0000;-0.0000;0")
				
would have the same result as the earlier example, but with zero components displaying as "0" rather than including extra zeroes to the right of the decimal point. If you use a multisection format, the formats for negative numbers must begin with a "-" (minus sign) as in the example.

Sample Visual Basic Procedure

Option Explicit
				
Function FormatComplex(NumToFormat As String, RealFormatCode As _
    String, ImagFormatCode As String)

    Dim PlusOrMinus As String
    Dim CharPosition As Integer

    ' Is NumToFormat real?
    If Right(NumToFormat, 1) <> "i" Then
        ' NumToFormat is real.
        FormatComplex = Format(NumToFormat, RealFormatCode)
    Else
        ' NumToFormat is either imaginary or complex.
        ' Search NumToFormat from right until + or - or left end is
        ' reached.
        PlusOrMinus = "not found"
        For CharPosition = Len(NumToFormat) - 1 To 1 Step -1
            PlusOrMinus = Mid(NumToFormat, CharPosition, 1)
            If PlusOrMinus = "+" Or PlusOrMinus = "-" Then Exit For
        Next
        ' Is NumToFormat complex or imaginary?
        If (PlusOrMinus = "+" Or PlusOrMinus = "-") And _
            CharPosition <> 1 Then
            ' NumToFormat is complex.
            ' Is imaginary component negative?
            If Mid(NumToFormat, CharPosition, _
                Len(NumToFormat) - CharPosition) < 0 Then
                ' Imaginary component is negative, so "-" does not need
                ' to be added.
                FormatComplex = Format(Left(NumToFormat, _
                    CharPosition - 1), RealFormatCode) & _
                    Format(Mid(NumToFormat, CharPosition, _
                    Len(NumToFormat) - CharPosition), _
                    ImagFormatCode) & "i"
            Else
                ' Imaginary component is not negative, so "+" needs to
                ' be added.
                FormatComplex = Format(Left(NumToFormat, _
                    CharPosition - 1), RealFormatCode) & "+" & _
                    Format(Mid(NumToFormat, CharPosition, _
                    Len(NumToFormat) - CharPosition), _
                    ImagFormatCode) & "i"
            End If
        Else
            ' NumToFormat is imaginary.
            FormatComplex = Format(Left(NumToFormat, _
                Len(NumToFormat) - 1), ImagFormatCode) & "i"
        End If
    End If
End Function
				

↑ Back to the top


References

For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
226118 OFF2000: Programming Resources for Visual Basic for Applications

↑ Back to the top


Keywords: KB213294, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213294
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 321