VBA Tip: Square Root / Square

Square Root

To get the square root of a number in VBA, you need to use the Sqr function or the exponent 0.5:

Sub example()

    number = 9
    
    MsgBox Sqr(number) 'Displays the square root: 3
    MsgBox number ^ 0.5 'Displays the square root: 3
    MsgBox number ^ (1 / 2) 'Displays the square root: 3

End Sub

Square

To get the square of a number, you simply use the ^ operator (as in cells):

Sub example()

    number = 9
    
    MsgBox number ^ 2 'Displays the square: 81

End Sub

Cube Root

To get the cube root of a number, use the exponent 1 / 3:

Sub example()

    number = 27

    MsgBox number ^ (1 / 3) 'Displays the cube root: 3

End Sub

Cube

To get the cube of a number, use the exponent 3:

Sub example()

    number = 9
    
    MsgBox number ^ 3 'Displays the cube: 729

End Sub