Excel.Tips.Net ExcelTips (Menu Interface)

Error Using ATAN2 Function in Macro

Lars ran into a problem using the ATAN2 function in a macro. He developed a rather complicated set of instructions, only to have VBA generate an error when it tried to use the ATAN2 function. He was able to simplify the macro so he could recreate the problem:

Sub Test()
    Dim A As Double

    Dim C As Double
    Dim E As Double

    A = 5908
    C = 0
    C = -C
    E = 180 / WorksheetFunction.Pi

    MsgBox E * WorksheetFunction.Atan2(C, A)
End Sub

When the code is executed, the error is generated on the line where ATAN2 is executed. Lars was wondering what, exactly, caused the problem.

The problem is apparently related to how you are manipulating the C variable. You first define C as zero, and then negate this value. There is no such thing as negative zero, and when you try to negate the value, Excel apparently balks when that value is subsequently used in the formula.

One way to solve the problem is simply to change the way in which C is transformed to account for zero values. Change the macro so that it looks like this:

Sub Test()
    Dim A As Double

    Dim C As Double
    Dim E As Double

    A = 5908
    C = 0
    If C <> 0 Then C = -C
    E = 180 / WorksheetFunction.Pi

    MsgBox E * WorksheetFunction.Atan2(C, A)
End Sub

Now the macro will work just fine because you are only doing the transform on C if it doesn't equal zero.

It also appears that the error is only generated if C is defined as a floating-point value. If you dimension C as an Integer, then the original macro does not generate an error. This could indicate that the problem is related to how a floating point representation of the non-existent negative zero is internally represented. Since the Integer data type deals strictly with whole numbers, that representation problem does not occur.

You also can get rid of the problem if you declare C as a Variant data type, or if you remove the declaration line altogether (which means that VBA defaults to declaring C as a Variant when it is first used).

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2892) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

Scott Renz    10 Mar 2014, 14:19
C = 0 - C

Works too.
Scott Renz    10 Mar 2014, 14:16
Changeing the line:
 C = -C
 C = -C + 0

Seems to avoid the error.

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2017 Sharon Parq Associates, Inc.