Results 1 to 7 of 7

How to square a number in VBA excel?

This is a discussion on How to square a number in VBA excel? within the Excel Questions forums, part of the Question Forums category; Hi how to sqUARE A NO. IN vba EXCEL?...

  1. #1
    New Member
    Join Date
    Sep 2006
    Posts
    46

    Default How to square a number in VBA excel?

    Hi how to sqUARE A NO. IN vba EXCEL?

  2. #2
    Board Regular mortgageman's Avatar
    Join Date
    Jun 2005
    Posts
    2,015

    Default

    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
    Gene, "The Mortgage Man", Klein
    See all my Mishegas Videos
    NEW: MY INTERNET RADIO SHOW:
    http://www.blogtalkradio.com/mortgageman

  3. #3
    Board Regular
    Join Date
    Mar 2006
    Location
    UK
    Posts
    986

    Default

    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
    Thanks Shippey

  4. #4
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    33,507

    Default

    .. or perhaps something like this?
    Sub Square()
    ****Selection.Offset(0, 1).Value = Selection.Value ^ 2
    End Sub
    Hope this helps, good luck.
    Peter
    Excel 2010 (mainly), 2016, 2013, 2007, 2003 - Windows 10, 7
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the VBHTML Maker
    - Read: Forum Rules & Forum Use Guidelines

  5. #5
    New Member
    Join Date
    Nov 2016
    Posts
    1

    Default Re: How to square a number in VBA excel?

    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.

  6. #6
    Board Regular
    Join Date
    Jan 2016
    Posts
    1,713

    Default Re: How to square a number in VBA excel?

    Quote Originally Posted by Greenapple757 View Post
    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 by 63falcondude; Nov 22nd, 2016 at 02:01 PM.

  7. #7
    Board Regular
    Join Date
    Mar 2014
    Posts
    1,426

    Default Re: How to square a number in VBA excel?

    Quote Originally Posted by Greenapple757 View Post
    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 by joeu2004; Nov 22nd, 2016 at 02:07 PM. Reason: "But if..."

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com