+ Reply to Thread
Results 1 to 18 of 18

VBA Function for Extrapolation

  1. #1
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    VBA Function for Extrapolation

    Hi all,

    I'm a VBA beginner and I'm trying to create a function in VBA for extrapolating the first, second, third or whatever ordinal number smaller than the max
    (for instance =TakeMinMax(“A1:A100”,3) would take the third number smaller than the max in the range A1:A100).

    Any ideas on how to do this?

    Thanks in advance.

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

    Re: VBA Function for Extrapolation

    Ignoring the programming language, what sort of algorithm would you apply?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    I wasn't aware that I'd need to use an algorithm for this... I have been given this exercise and as a VBA beginner I'm not certain that I fully understand what I need to do to create that function.

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

    Re: VBA Function for Extrapolation

    An alorithm is just a sequence of steps to achieve a result. How would you do it on paper?
    Last edited by shg; 03-29-2019 at 01:22 PM.

  5. #5
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    I'd find the maximum in a data set (for example Q1 Sales). Then write a function for extrapolating this data (except the maximum) to find Q2 Sales. I'm stuck on how to write this function.

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

    Re: VBA Function for Extrapolation

    Forget what the numbers mean.

    You have a list of 100 random numbers, perhaps including some duplicates, paper, and a pencil. How would you find the number you're looking for?

    BTW, this has nothing to do with extrapolation.

  7. #7
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    I'd rewrite the numbers in a decreasing order. The maximum would be at the top. The number that's one row down from the maximum would be the first ordinal number that's smaller than maximum, the number that's two rows down from the max would be the second ordinal number that's smaller than max and so on. Is that the right way of doing it?

    I initially thought that the function would be =TakeMinMax("A1:A100",<Max), but this seems too simple.

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

    Re: VBA Function for Extrapolation

    What if, after sorting, the numbers are 100, 100, 99, 98, 98, 95, 94, ...

    What's the answer and why? Does it change your algorithm?

  9. #9
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    I'd need to know if having duplicates is a problem for this task. If it doesn't matter, then no adjustment would be needed to the algorithm, except that the first ordinal number smaller than the max would be 99, and not 100, and so on.

    Alternatively, I could come up with a way to add 0.5 to one of the duplicates (assuming there is only one duplicate). Although this would get complicated if there were more than one duplicate.

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

    Re: VBA Function for Extrapolation

    If you need not consider duplicates, then

    =LARGE(A1:A100, 3)
    =SMALL(A1:A100, 3)

  11. #11
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    How can I create a function like that in VBA? I've been trying to do that, but no luck so far. This is what I've tried:

    Function TakeMinMax(r As Range, i As Integer) As Integer
    Set r = "A2:A21"

    TakeMinMax = Large(r, i)

    End Function

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

    Re: VBA Function for Extrapolation

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA Function for Extrapolation

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  14. #14
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    Thank you. When I run this code, a macro window comes up asking to create a macro. Not sure why. So I'm not able to run the code. What am I doing wrong?

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

    Re: VBA Function for Extrapolation

    1. Copy the code from the post

    2. In Excel, press Alt+F11 to open the Visual Basic Editor (VBE)

    3. From the menu bar in the VBE window, do Insert > Module

    4. Paste the code in the window that opens

    5. Press Alt+Q to close the VBE and return to Excel

  16. #16
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    I ran both codes (from post 12 and 13) in a module window and this has created two formulas in Excel. However, when I use the formulas to show the ordinal number from the range in Excel, I get zero in both cases. This is the formula in Excel: =TakeMinMax(A2:A21,3). What am I doing wrong?

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

    Re: VBA Function for Extrapolation

    Dunno. Post the workbook.

  18. #18
    Registered User
    Join Date
    03-29-2019
    Location
    London
    MS-Off Ver
    Office365
    Posts
    9

    Re: VBA Function for Extrapolation

    It works now. I didn't realise that the function name in Sheet 1 code has to be the same as in Module 1 code for it to work. Thank you very much for your help!

+ 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. Extrapolation
    By aqi443 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 11-14-2017, 09:23 AM
  2. Extrapolation graph
    By Mesut1337 in forum Excel Charting & Pivots
    Replies: 11
    Last Post: 05-12-2016, 08:50 AM
  3. Extrapolation Calculator
    By sinikiwep in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-11-2014, 09:27 AM
  4. Extrapolation formula?
    By wonderdunder in forum Excel General
    Replies: 11
    Last Post: 03-25-2011, 11:03 AM
  5. Replies: 0
    Last Post: 07-24-2008, 02:27 PM
  6. Automating Extrapolation
    By smurray444 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-29-2006, 09:20 AM
  7. Automating Extrapolation
    By smurray444 in forum Excel General
    Replies: 1
    Last Post: 01-29-2006, 09:09 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