Consulting

Results 1 to 6 of 6

Thread: Coding Help for Range Calculations

  1. #1

    Coding Help for Range Calculations

    I'm trying to write a VBA code that calculates mean bearing of a group of bearings and distances. The Input needs to be a Range.

    Bearing Distance
    N 80-05-36 E 664.62
    S 88-58-12 E 2640.00
    S 88-58-12 E 2517.90

    I already have a code that converts the bearings to Azimuths in decimal degrees (i.e. N 88-00-00 E = 88.00000) and I get the overall idea of what I need to do. Each Bearing needs to be multiplied by the distance and this is done for each row. Those are added together and then divided by the sum of all the distances. My question is...Is their a way to write the code so that I don't need to call a specific cell for the calculations. For example: I don't want to have to say like
    ((A1*A2)+(B1*B2))/(B1+B2). I'm hoping to be able to write out the bearings and distances in any two columns on my sheet, input the range into the function and have the mean bearing output be calculated. If you can help steer me in the right direction, I'd appreciate it.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    What does "Mean Bearing" mean? Is it the bearing from start point to end point?

    Also, please explain how to interpret a Bearing in the form "N/S compass reading E/W"
    Last edited by SamT; 03-09-2017 at 08:17 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Is their a way to write the code so that I don't need to call a specific cell for the calculations.
    This code goes in a standard module

    You'll have to fill in the blanks, (Bearing2Azimuth,)
    Public Function AK_Beaver(Inputs As Range) As 'Unknown, Fill in the Blanks
    Dim rw As Range
    Dim Dividend As Double
    Dim Divisor As Double
    Dim Result as double
    
    For Each rw In Inputs.Rows
      Dividend = Dividend + Bearing2Azimuth(rw.Cells(1)) * rw.Cells(2)
      Divisor = Divisor + rw.Cells(2)
    Next
    
    Result = Dividend / Divisor
    
    NewResult = 'Fill in the blanks here
    
    AK_Beaver = NewResult  
    End Function
    To use, In your results cell, enter the formula "=AK_Beaver(Select your range here)"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    My question is...Is their a way to write the code so that I don't need to call a specific cell for the calculations.

    For example: I don't want to have to say like ((A1*A2)+(B1*B2))/(B1+B2).

    I'm hoping to be able to write out the bearings and distances in any two columns on my sheet, input the range into the function and have the mean bearing output be calculated.

    If you can help steer me in the right direction, I'd appreciate it.
    "Steer me"

    I 'think' you want a VBA User Defined Function (UDF) that returns a Double that you can use in a worksheet

    Function AZ (R as Range) As Double
    You example has 3 rows, but you example formula only uses 2 (1 and 2) so I'm not sure about what you want back

    Provide more detail and the calculations if you can
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Thanks everyone for your help. SamT, that did the trick. I was getting an error message at first and was about to post for more help, but I told myself to keep trying and low and behold I debugged the problem! I'm getting better that's for sure. I am extremely thankful for this forum, that's for sure.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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