ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   bizarre "invalid procedure call" error (https://www.excelbanter.com/excel-programming/303861-bizarre-invalid-procedure-call-error.html)

PatFinegan[_14_]

bizarre "invalid procedure call" error
 
Sub Test1 below generates an "invalid procedure call or argument" erro
(#5) in Excel 97 and Excel 2003 on all of my PCs. Sub Test2 does not
Can anyone replicate this? If so, can anyone explain the problem, o
provide a workaround other than writing the variables to cells an
performing the calculations there? Thanks in advance.

Sub Test1()
Dim a As Double, b As Double, c As Double
a = -0.1
b = 0.05
c = a ^ b
End Sub

Sub Test2()
Dim c as Double
c = -0.1 ^ 0.05
End Su

--
Message posted from http://www.ExcelForum.com


PatFinegan[_15_]

bizarre "invalid procedure call" error
 
Just to clarify, the problem arises whether or not the answer is a
imaginary number. So, for example, -1 = -1 ^ (1/3) involves n
imaginary numbers, but will generate a runtime error when I assign -
and 1/3 to separate variables.

Sub test()
Dim a As Double, b As Double, c As Double
c = -1 ^ (1 / 3) 'Performs fine.
a = -1
b = 1 / 3
c = a ^ b 'Crashes.
End Su

--
Message posted from http://www.ExcelForum.com


Don Guillett[_4_]

bizarre "invalid procedure call" error
 
Can't tell you why but the problem was with the -
This worked

Sub Test1()
Dim a As Double, b As Double, c As Double
a = 0.1
b = 0.05
c = -a ^ b
MsgBox c
End Sub
--
Don Guillett
SalesAid Software

"PatFinegan " wrote in message
...
Sub Test1 below generates an "invalid procedure call or argument" error
(#5) in Excel 97 and Excel 2003 on all of my PCs. Sub Test2 does not.
Can anyone replicate this? If so, can anyone explain the problem, or
provide a workaround other than writing the variables to cells and
performing the calculations there? Thanks in advance.

Sub Test1()
Dim a As Double, b As Double, c As Double
a = -0.1
b = 0.05
c = a ^ b
End Sub

Sub Test2()
Dim c as Double
c = -0.1 ^ 0.05
End Sub


---
Message posted from
http://www.ExcelForum.com/




kkknie[_160_]

bizarre "invalid procedure call" error
 
Not sure how do a FRACTIONAL exponent for any negative number without
getting an imaginary result. What the code you posted is doing is the
following:

c = -1 ^ (1 / 3) 'Performs fine.

First take 1/3 to get 0.333333...
Then raise 1 to the 0.333333 to get 1
Then negate it.

You can duplicate the error if you change your formula to read:

c = (-1) ^ (1 / 3) 'Error

K


---
Message posted from http://www.ExcelForum.com/


Nick Hodge

bizarre "invalid procedure call" error
 
Pat

Replicate...yes...reason no.... It appears to be the - sign in the
variable. remove the sign and all is well. Negate it later and all is well
too

Sub Test1()
Dim a As Double, b As Double, c As Double
a = 0.1
b = 0.05
c = -(a ^ b)
Debug.Print c
End Sub

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


"PatFinegan " wrote in message
...
Sub Test1 below generates an "invalid procedure call or argument" error
(#5) in Excel 97 and Excel 2003 on all of my PCs. Sub Test2 does not.
Can anyone replicate this? If so, can anyone explain the problem, or
provide a workaround other than writing the variables to cells and
performing the calculations there? Thanks in advance.

Sub Test1()
Dim a As Double, b As Double, c As Double
a = -0.1
b = 0.05
c = a ^ b
End Sub

Sub Test2()
Dim c as Double
c = -0.1 ^ 0.05
End Sub


---
Message posted from
http://www.ExcelForum.com/




PatFinegan[_16_]

bizarre "invalid procedure call" error
 
Thanks, kkknie, but a fractional root whose reciprocal is an odd intege
(as opposed to an even integer) should not generate an error on
negative number.

For example: -8 = -2 ^ 3
Therefo -2 = -8 ^ (1/3)

I added an error handler to my second post, showing how Excel handle
the equation correctly on a worksheet, but not in VBA

--
Message posted from http://www.ExcelForum.com


PatFinegan[_17_]

bizarre "invalid procedure call" error
 
Thanks, Nick, for the simple workaround. Strange discrepency betwee
Excel and VBA, though

--
Message posted from http://www.ExcelForum.com


Tom Ogilvy

bizarre "invalid procedure call" error
 
it appears to be a precedence/order of operations problem. If you put
parens around -0.1 you get the same problem:

Sub Test2()
Dim c As Double
c = (-0.1) ^ 0.05
End Sub


Exponention appears to have a higher precedence than negation.

--
Regards,
Tom Ogilvy


"PatFinegan " wrote in message
...
Sub Test1 below generates an "invalid procedure call or argument" error
(#5) in Excel 97 and Excel 2003 on all of my PCs. Sub Test2 does not.
Can anyone replicate this? If so, can anyone explain the problem, or
provide a workaround other than writing the variables to cells and
performing the calculations there? Thanks in advance.

Sub Test1()
Dim a As Double, b As Double, c As Double
a = -0.1
b = 0.05
c = a ^ b
End Sub

Sub Test2()
Dim c as Double
c = -0.1 ^ 0.05
End Sub


---
Message posted from http://www.ExcelForum.com/




Tom Ogilvy

bizarre "invalid procedure call" error
 
From help for precedence

Arithmetic
Exponentiation (^)
Negation (-)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
String concatenation (&)



--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
it appears to be a precedence/order of operations problem. If you put
parens around -0.1 you get the same problem:

Sub Test2()
Dim c As Double
c = (-0.1) ^ 0.05
End Sub


Exponention appears to have a higher precedence than negation.

--
Regards,
Tom Ogilvy


"PatFinegan " wrote in message
...
Sub Test1 below generates an "invalid procedure call or argument" error
(#5) in Excel 97 and Excel 2003 on all of my PCs. Sub Test2 does not.
Can anyone replicate this? If so, can anyone explain the problem, or
provide a workaround other than writing the variables to cells and
performing the calculations there? Thanks in advance.

Sub Test1()
Dim a As Double, b As Double, c As Double
a = -0.1
b = 0.05
c = a ^ b
End Sub

Sub Test2()
Dim c as Double
c = -0.1 ^ 0.05
End Sub


---
Message posted from http://www.ExcelForum.com/






Tom Ogilvy

bizarre "invalid procedure call" error
 
if you put -1 (A1) in one cell and .05 (a2) in another

=A1^A2

raises an error as well.

=-1^0.5 raises an error as well

---------------------------------------

In a worksheet, the order of operations is different:

: (colon)
(single space)

, (comma)
Reference operators
- Negation (as in -1)
% Percent
^ Exponentiation
* and / Multiplication and division
+ and - Addition and subtraction
& Connects two strings of text (concatenation)
= < <= = < Comparison


--
Regards,
Tom Ogilvy


"PatFinegan " wrote in message
...
Thanks, Nick, for the simple workaround. Strange discrepency between
Excel and VBA, though.


---
Message posted from http://www.ExcelForum.com/




PatFinegan[_18_]

bizarre "invalid procedure call" error
 
Dear Tom,

Thanks for pointing out the difference between orders of operation i
VBA and Excel.

However, to clarify, my first post c= -1^.5 should indeed hav
generated an error because the square root of a negative number i
imaginary. I immediately regretted posting it because it cluttered tw
very distinct issues -- the handling of imaginary numbers and
computational inconsistency between Excel and VBA.

That's why I added a second equation, c = -1 ^ (1/3), because the cub
root of a negative number is not imaginary and should therefore no
result in an error. But as I wrote, the equation worked fine in a
Excel spreadsheet but bombed in VBA. It is that latter computationa
discrepency that I find troubling, since the equation is definitel
"solvable". Sorry if that wasn't clear

--
Message posted from http://www.ExcelForum.com


Tom Ogilvy

bizarre "invalid procedure call" error
 
Just because the equation is solvable doesn't mean it doesn't deal in the
realm of imaginary numbers. Since only a subset of roots of negative real
numbers would result in non-imaginary numbers, I think it is clear that the
authors of VBA have chosen to error on such a construct. While this may be
inconsistent with Excel, there is no reason to expect them to be the same in
this gray area - VBA is a separate product that has been coupled with Excel.
They don't share common code.

--
Regards,
Tom Ogilvy

"PatFinegan " wrote in message
...
Dear Tom,

Thanks for pointing out the difference between orders of operation in
VBA and Excel.

However, to clarify, my first post c= -1^.5 should indeed have
generated an error because the square root of a negative number is
imaginary. I immediately regretted posting it because it cluttered two
very distinct issues -- the handling of imaginary numbers and a
computational inconsistency between Excel and VBA.

That's why I added a second equation, c = -1 ^ (1/3), because the cube
root of a negative number is not imaginary and should therefore not
result in an error. But as I wrote, the equation worked fine in an
Excel spreadsheet but bombed in VBA. It is that latter computational
discrepency that I find troubling, since the equation is definitely
"solvable". Sorry if that wasn't clear.


---
Message posted from http://www.ExcelForum.com/





All times are GMT +1. The time now is 02:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com