+ Reply to Thread
Results 1 to 11 of 11

Convert some formulas to one UDF Function

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Convert some formulas to one UDF Function

    Hello everybody
    I have data in columns (B:E) and in column G & I & K there are formulas that lead to specific result
    I need to convert these formulas to UDF function not code ...

    Attention: range("B2:E2") is just an example .. I mean the range may be dynamic .. may be any range in the same row,eg. may be (A2:J2)
    Hope it is clear
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Convert some formulas to one UDF Function

    Here's the first one:

    Public Function MultiTan(ParamRng As Excel.Range) As Double
        Const PI = 3.1415927
        Dim Cell As Excel.Range
        Dim Ret As Double
        Ret = 0
        For Each Cell In ParamRng
            Ret = Ret + Tan(Cell.Value * PI / 180)
        Next
        Ret = Atn(Ret)
        MultiTan = Ret
    End Function
    Enter in G2 as
    =MultiTan(B2:E2)

  3. #3
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Convert some formulas to one UDF Function

    The complete calc:
    Public Function FullCalc(ParamRng As Excel.Range) As Double
        Const PI = 3.1415927
        Dim Cell As Excel.Range
        Dim Ret As Double
        Ret = 0
        For Each Cell In ParamRng
            Ret = Ret + Tan(Cell.Value * PI / 180)
        Next
        Ret = Atn(Ret) * 180 / PI
        If Ret < 0 Then Ret = Ret + 360
        FullCalc = Ret
    End Function
    Last edited by cyiangou; 08-04-2015 at 04:18 PM.

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Convert some formulas to one UDF Function

    Thanks for offering help
    But I can't get my desired results
    I could devise it now
    Function AnglesAverage(Rng As Range)
        Application.Volatile
        Dim Cell As Range, Counter As Long, Temp
        For Each Cell In Rng
            Temp = Temp + Tan(Cell.Value * Application.WorksheetFunction.PI() / 180)
        Next Cell
        Temp = Atn(Temp) * 180 / Application.WorksheetFunction.PI()
        If Temp <= 0 Then Temp = Temp + 360
        AnglesAverage = Temp
    End Function

  5. #5
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Convert some formulas to one UDF Function

    At first I thought you were looking for a set of 3 formulas.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Convert some formulas to one UDF Function

    Thanks a lot for help
    In your post #3
    Your UDF function is very near of mine .. although I have got different results ... Any idea about that?

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: Convert some formulas to one UDF Function

    You call the function "anglesaverage", suggesting that this function is supposed to give some kind of "average" of the four input angles. Note that, by using the built in VBA function Atn(), your results will always be in the first or fourth quadrant (I'm assuming you are familiar with the unit circle here). Based on the given inputs, where three of four inputs for each computation are in the first and fourth quadrant, perhaps this is sufficient. I guess I would suggest that you review the trigonometry behind the problem and make certain that you will never have a scenario where the result should be in the second and third quadrants before accepting this as a solution.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Convert some formulas to one UDF Function

    maybe so
    Function AnglesAverage(Rng As Range) As Double
    Dim v, t#
    With WorksheetFunction
        For Each v In Rng.Value
            t = t + Tan(.Radians(v))
        Next
        t = Atn(t) * 180 / .Pi
    End With
    AnglesAverage = IIf(t < 0, t + 360, t)
    End Function
    edited
    Oh, it was in the post #3
    Last edited by nilem; 08-04-2015 at 04:50 PM.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Convert some formulas to one UDF Function

    How about just a formula?

    Row\Col
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    2
    297
    271
    160
    350
    -1.554
    -89.042
    270.958
    270.958
    M2: =MOD(DEGREES(ATAN(SUMPRODUCT(TAN(RADIANS(B2:E2))))), 360)
    3
    300
    273
    161
    351
    -1.524
    -87.314
    272.686
    272.686
    4
    303
    275
    162
    352
    -1.497
    -85.743
    274.257
    274.257
    5
    306
    277
    163
    353
    -1.471
    -84.260
    275.740
    275.740


    I can't imagine what the formula usefully computes.
    Last edited by shg; 08-04-2015 at 06:00 PM.
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Convert some formulas to one UDF Function

    @Mr. shg
    Excellent formula
    Great solution thanks a lot for sharing this solution
    Regards

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Convert some formulas to one UDF Function

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Convert a group of formulas to array formulas at once
    By coach.32 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-16-2015, 02:21 AM
  2. Convert Formulas into VBA
    By timmyjc18 in forum Excel General
    Replies: 3
    Last Post: 06-20-2012, 07:40 PM
  3. convert formulas into VBA
    By clausule in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-26-2011, 09:29 AM
  4. what function to convert convert 34234 to 99999
    By archiles in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-04-2008, 11:26 PM
  5. Convert formulas in text into formulas
    By Tau in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-13-2007, 08:31 AM
  6. How to convert 1 500 to 1,500 to use in formulas
    By dstock in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-21-2007, 09:50 AM
  7. [SOLVED] I get my formulas like this RC[-1]*R[-2]C[6] how could I convert .
    By nperl in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-12-2005, 03:06 AM

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