1
N1 = 3
N2 = 4
N3 = Sqr(N1^(2) + N2^(2)) 'N3 is the hypotenuse
MsgBox N3

I get the following error and N1^ is highlight.

Bhavesh Shaha
  • 717
  • 5
  • 21
  • 2
    Runs just fine when I test it like that, so you must have other code that is declaring the variables as something incorrect. Try using `Option Explicit` and define all of your variable appropriately. – braX Apr 21 '20 at 10:42
  • 3
    Seems like Microsoft introduced a bug with 64-bit VBA when some programmer decided that `^` was a character which could be safely used for `Long Long` type declarations. It is a mystery why anyone thought that there was a need to have any type declaration character for a new data type since such ways of declaring type is ancient cruft in VBA, and it is even more of a mystery why someone thought that the exponentiation operator would be a good choice. An astonishingly bad idea. – John Coleman Apr 21 '20 at 11:18

1 Answers1

1

There seems to be some issues with the power operator, please refer to this thread:

VBA power operator (^) not working as expected in 64-bit VBA

Also temporary fix to this issue mentioned in this thread, use the below function to calculate power:

Excel.WorksheetFunction.Power(N1, 2)

Edit: for Powerpoint VBA, I'm not sure if there's a library function that would calculate the power, but for integer type of power number, we can simply wrap a loop into a function:

Public Function myPower(base As Double, pow As Long) As Double
    Dim ret As Double: ret = 1

    If pow > 0 Then
        For i = 1 To pow
            ret = ret * base
        Next i
    Else
        For i = -1 To pow Step -1
            ret = ret / base
        Next i
    End If

    myPower = ret
End Function

For floating point power number... I'll have to go back and check the formula....


Second Edit: I just tested putting parenthesis on the variables then using caret, seems to be working on my 64bit Excel, maybe you should try this in PowerPoint first :)

Public Sub test()
    N1 = 3
    N2 = 4
    N3 = Sqr((N1) ^ 2 + (N2) ^ 2)
    MsgBox N3
End Sub
Luan Yi
  • 111
  • 1
  • 4