+ Reply to Thread
Results 1 to 3 of 3

Populating a column with an equation written in vba

  1. #1
    Registered User
    Join Date
    06-19-2012
    Location
    inverness
    MS-Off Ver
    Excel 2007
    Posts
    26

    Populating a column with an equation written in vba

    Hey,

    My problem seems relatively straight forward, however, as im new to vba coding im having a little difficulty here.

    I want to calculate the length of the hypotenuse of a triangle, where column M is my x-vector, and column N is my y-vector. I want the hypotenuse length to be stored in column O.
    I have put the following code into VBA and because i have fixed M2 and N2 within the equation every single row in column O displays the same number. Therefore i would like to learn how i can change M2 to M3,M4,M5,.... aswell as the N2 to N3,N4,N5,... . This is simple to do within the excel spreadsheet itself by just filling the cells down. I just dont know how to do it in VBA.

    "=SQRT((M2^2)+(N2^2))"

    The code that this equation sits in is:

    Sub avg()
    Dim Rng As Range
    Dim n As Integer
    Dim c As Integer
    Dim Div As Integer
    Div = Range("K2")
    c = 1
    Set Rng = Range("F3:F300")
    If Div <= Rng.Count Then
    For n = 3 To Rng.Count Step Div
    c = c + 1
    Cells(c, "L") = Application.Sum(Range("F" & n).Resize(Div)) / Div
    Cells(c, "M") = Application.Sum(Range("H" & n).Resize(Div))
    Cells(c, "N") = Application.Sum(Range("I" & n).Resize(Div))
    Cells(c, "O").Formula = "=SQRT((M2^2)+(N2^2))"
    Next n
    End If
    End Sub

    Any help would be greatly apreciated,

    Cheers, from sunny scotland

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Populating a column with an equation written in vba

    Perhaps better success with the R1C1 notation:

    Please Login or Register  to view this content.

    As per forum rules (link above), you should edit that post above and add code tags.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Populating a column with an equation written in vba

    Try this:
    Please Login or Register  to view this content.
    If this was helpful, please don't forget to click on the star below the posting and mark your question as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1