Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Square the values of individual cells in a range

Latest post Thu, May 22 2008 6:43 AM by Bob Phillips. 13 replies.
  • Mon, May 19 2008 3:49 AM

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Square the values of individual cells in a range

    In the range A1:E1 are the values 1,2,3,4,5

    and in the range A3:E3 the array formula {=A1:E1^2} witch gives me the values 1,4,9,16,25

    Is there a way in vba to do the same in a single statement.....?

    • Post Points: 21
  • Mon, May 19 2008 4:42 AM In reply to

    • Roger Govier
    • Top 10 Contributor
    • Joined on Wed, Jan 9 2008
    • Abergavenny, South Wales, UK
    • Posts 60
    • Points 1,017
    • MVP

    Re: Square the values of individual cells in a range

     Try

    Range("A3:E3").FormulaArray = "=R[-2]C:R[-2]C[4]^2"

    Regards Roger Govier Microsoft Excel MVP
    • Post Points: 37
  • Mon, May 19 2008 5:28 AM In reply to

    Re: Square the values of individual cells in a range

    Why are you using an array formula, why not just

     

    Range("A3:E3").Formular1c1 = "=R[-2]C^2"

    Regards

    Bob

    • Post Points: 5
  • Mon, May 19 2008 5:49 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Re: Square the values of individual cells in a range

     thnx Roger

    But I want to use it like this

    Function TestFunction(counts As Range, values As Range)

           With WorksheetFunction

                 dTemp = .SUM(.MMULT(values^2,.TRANSPOSE(counts)))

           End With

    End Function

    • Post Points: 21
  • Mon, May 19 2008 9:13 AM In reply to

    Re: Square the values of individual cells in a range

    Function TestFunction(counts As Range, values As Range)

        With Application.Caller.Parent

            TestFunction = .Evaluate("MMult(" & values.Address & _ "^2,Transpose(" & counts.Address & "))")

        End With

    End Function

    Regards

    Bob

    • Post Points: 21
  • Tue, May 20 2008 2:53 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Re: Square the values of individual cells in a range

     Thnx Bob,

    But why do you use Application.Caller.Parent....?

    In my code

    Evaluate("SUM(MMULT(" & Values.Address & "^2,TRANSPOSE(" & Counts.Address & ")))")

    will work fine

    Jelle-Jeroen

    • Post Points: 21
  • Tue, May 20 2008 3:09 AM In reply to

    Re: Square the values of individual cells in a range

    It will if the active sheet is a worksheet when you run the code, but if it is a chart sheet ...

    Using APplication.Caller.Parent ensures that the Evaluate works against the worksheet that the function is called from.

    Regards

    Bob

    • Post Points: 5
  • Tue, May 20 2008 3:15 AM In reply to

    Re: Square the values of individual cells in a range

    BTW, you do not need the SUM, as per your example. MMULT returns an array equal to the dimensions of your input arrays, which were only 1 row 1 column in your example.

    Regards

    Bob

    • Post Points: 21
  • Tue, May 20 2008 3:26 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Re: Square the values of individual cells in a range

    Bob,

    True but I simplyfied the example.

    I will post the complete problem soon.

    Jelle-Jeroen

    • Post Points: 21
  • Tue, May 20 2008 4:09 AM In reply to

    Re: Square the values of individual cells in a range

    I thought that was the case, but I decided to mention it anyway.

    Regards

    Bob

    • Post Points: 21
  • Tue, May 20 2008 4:30 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Re: Square the values of individual cells in a range

     Bob,

    I posted the complete thing at http://xlns.lamkamp.nl/

    I want to post it also on the Excel User Group...

    Is that a good plan or is it not done....?

     

    Jelle-Jeroen

    • Post Points: 21
  • Tue, May 20 2008 5:10 AM In reply to

    Re: Square the values of individual cells in a range

    Can't see a problem, but as you have the link, probably not necessary.

     

    As an aside, the 'With WorksheetFunction' is superfluous in the 2nd solution (removing it might make it as fast Smile

     

    Regards

    Bob

    • Post Points: 21
  • Thu, May 22 2008 6:14 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 34
    • Points 613

    Re: Square the values of individual cells in a range

    Bob,

    Evaluate("SUM(MMULT(" & Values.Address & "^2,TRANSPOSE(" & Counts.Address & ")))")

    doesn't seem to work if the data is on another sheet as the formula...

    do you know a workaround for that...

     

    Jelle-Jeroen 

    • Post Points: 21
  • Thu, May 22 2008 6:43 AM In reply to

    Re: Square the values of individual cells in a range

    Sure 

     

    Function TestFunction(counts As Range, values As Range)

        With Application.Caller.Parent

            TestFunction = .Evaluate("MMult(" & values.Address(, , , True) & _
                                     "^2,Transpose(" & counts.Address(, , , True) & "))")
           
        End With

    End Function

    Regards

    Bob

    • Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.