How to square a number in VBA excel?

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
 
Upvote 0
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
 
Upvote 0
.. 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>
 
Upvote 0
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.
 
Upvote 0
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:
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,559
Latest member
MrPJ_Harper

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
Back
Top