Error Using ATAN2 Function in Macro

Written by Allen Wyatt (last updated October 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


1

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).

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Hiding Formatting Changes in Track Changes

Word can easily (and handily) keep track of changes you make in your document. You may not want all your changes tracked, ...

Discover More

Creating Additional Clocks

Want to keep track of what time it is in various areas of the world? Windows makes it easy by allowing you to display ...

Discover More

Making Custom Heading Styles Appear in the Navigation Pane

Creating styles for your documents is a powerful way to format them. How do you get paragraphs formatting with those ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected ...

Discover More

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Automatically Changing References to VBA Libraries

VBA libraries are used to provide some functions to your macros. These libraries are often tied to the version of Excel ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2023-03-03 10:36:05

Brian Murphy

VBA run time errors can happen when either argument is some variation of zero, for example -0. Try this: WorksheetFunction.Atan2(C + 0, A + 0)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.