How to square a number in VBA excel?

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Not tested, but not much to go wrong with this either

public function sqnum (num2bsquared as double) as double
sqnum=num2bsquared * num2bsquared
end function

Gene, "The Mortgage Man", Klein
 

shippey121

Well-known Member
Joined
Mar 21, 2006
Messages
1,001
we will need more information on what you are wanting to do,

like where do you get the number from that you are wanting to SQUARE and where do you want the result to be

the code using an input and a message box for the result would be#

Code:
Sub squre()
x = 0
x = InputBox("enter number")
z = x * x
MsgBox ("the Squre of " & x & " is " & z)

End Sub

HTH
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
49,430
Office Version
  1. 365
Platform
  1. Windows
.. or perhaps something like this?
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Square()
    Selection.Offset(0, 1).Value = Selection.Value ^ 2
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

Greenapple757

New Member
Joined
Nov 21, 2016
Messages
1

ADVERTISEMENT

Hello, I have a problem similar,
Example B5 is Range2, And B3 is Range1
I have and ActiveX button on the sheet that I need to add VBA code to do the following:
When the Command button is clicked, the number entered in "B5" Range2, should be squared and the results shown in "B3" Range1

Example Range2 "B5" has 22, then when command button is clicked, Range1 "B3" should show the squared value of 484.
 

63falcondude

Well-known Member
Joined
Jan 15, 2016
Messages
3,572
Hello, I have a problem similar,
Example B5 is Range2, And B3 is Range1
I have and ActiveX button on the sheet that I need to add VBA code to do the following:
When the Command button is clicked, the number entered in "B5" Range2, should be squared and the results shown in "B3" Range1

Example Range2 "B5" has 22, then when command button is clicked, Range1 "B3" should show the squared value of 484.

Try

Code:
Private Sub CommandButton1_Click()


Range("B2").Value = Evaluate("B5^2")


End Sub
 
Last edited:

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
3,080
Office Version
  1. 2010
Platform
  1. Windows
Example B5 is Range2, And B3 is Range1
[....] the number entered in "B5" Range2, should be squared and the results shown in "B3" Range1
Example Range2 "B5" has 22, then when command button is clicked, Range1 "B3" should show the squared value of 484.

I think you mean that the variables Range1 and Range2 contain the strings "B3" and "B5" respectively. In that case:

Range(Range1) = Range(Range2)^2

But if you mean that you have Set Range1 = Range("B3") and similarly for Range2, then try:

Range1.Value = Range2^2

If that does not work, I suggest that you post the actual relevant VBA code, including declarations, instead of describing it.
 
Last edited:

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,344
Office Version
  1. 365
Platform
  1. Windows
deleted
 
Last edited:

Forum statistics

Threads
1,136,426
Messages
5,675,773
Members
419,585
Latest member
popsin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top