+ Reply to Thread
Results 1 to 12 of 12

VBA code to select Maximum record for each year and sort

  1. #1
    Registered User
    Join Date
    06-02-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    80

    Smile VBA code to select Maximum record for each year and sort

    I have wind data for many years (Data worksheet). The data includes the year, wind speed and direction.

    I need a VBA code to scan the data and create the following:
    • First: The maximum wind speed per year (example shows in "Annual Maxima worksheet", labeled as (1)
    • Second: Same as above, but split the data by direction (example shows in "Annual Maxima worksheet", labeled as (2)
    • Third: Sort the data above in Descending order (example shows in "Annual Maxima worksheet", labeled as (3)


    I need the code to by dynamic identifies how many years and set the data properly regardless to how many years of data

    I appreciate any help
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,512

    Re: VBA code to select Maximum record for each year and sort

    Please Login or Register  to view this content.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: VBA code to select Maximum record for each year and sort

    You don't need VBA - you can use formulas, as in the attached. Since you are using 2010, the formulas are a little more complex than what you would use in 365 - and they are array-entered using Ctrl-Shift-Enter instead of a simple enter.
    Attached Files Attached Files
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Registered User
    Join Date
    06-02-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA code to select Maximum record for each year and sort

    @daboho
    Thanks for your help... I see this attempt to help with No. one of my request... One little problem is that, although it works well with picking the maximum wind speed for each year, but the corresponding wind direction is not correct. I need to pick up the direction which is associated with the selected speed (in the same line)

    Any idea on sorting the wind speed in descending order (per no. 3 of my question)... Thanks!

  5. #5
    Registered User
    Join Date
    06-02-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA code to select Maximum record for each year and sort

    @Bernie Deitrick
    Thanks for your help. I prefer the VBA so I do not have to copy the formulas If I have a larger dataset with more or less years for example

    However, I was interested to explore the formulas as an option, while it works fine with my number 1 request, My second request is not working properly. The reason is the direction in the header is not just the posted angle, rather it is a range of angles, which I showed on the side table in orange... This means as an example" the angle of 120, should pick up all angles from 106 to 135 per the table in orange, and so on...

    I appreciate if you can correct for this...

    Thanks a lot!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: VBA code to select Maximum record for each year and sort

    "the angle of 120, should pick up all angles from 106 to 135"

    That is what the helper column of formulas do.

    As far as maintaining the formulas, as long as you insert enough rows within your existing data set to allow the new data set to be added within or to over-write the old set, the formulas will expand and all will be good.
    Last edited by Bernie Deitrick; 05-08-2024 at 06:03 PM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA code to select Maximum record for each year and sort

    See if this works.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,512

    Re: VBA code to select Maximum record for each year and sort

    try this only little different value with you see picture yellow colour in above
    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by daboho; 05-10-2024 at 01:49 AM.

  9. #9
    Registered User
    Join Date
    06-02-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA code to select Maximum record for each year and sort

    Quote Originally Posted by Bernie Deitrick View Post
    "the angle of 120, should pick up all angles from 106 to 135"

    That is what the helper column of formulas do.

    As far as maintaining the formulas, as long as you insert enough rows within your existing data set to allow the new data set to be added within or to over-write the old set, the formulas will expand and all will be good.
    @Bernie Deitrick

    Thanks a lot, working fine except for one minor thing:
    For the direction 345, the helper column calculates it as 0, while it should be 330 ...
    looks like a minor adjustment to the formula to consider this minor boundary is needed if possible... Thanks a lot

    Attachment 869016

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: VBA code to select Maximum record for each year and sort

    Change the formula in H2 to this, then copy down:

    =IFERROR(MAX(0,MOD(IFERROR(MROUND(G2-1,30),MROUND(LEFT(G2,FIND(" ",G2)-1)-1,30)),360)),0)

  11. #11
    Registered User
    Join Date
    06-02-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA code to select Maximum record for each year and sort

    Thanks all for your help

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: VBA code to select Maximum record for each year and sort

    You're quite welcome - and thanks for the rep!

+ 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] Get maximum value for unique record in three column
    By pnnaik in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-16-2015, 02:19 PM
  2. [SOLVED] Name of maximum seller per year
    By makinmomb in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-13-2014, 01:36 PM
  3. [SOLVED] How to extract maximum record from dataset based on multiple criteria?
    By adelkam in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-15-2013, 04:41 AM
  4. Maximum value per year
    By tanyastories in forum Excel General
    Replies: 2
    Last Post: 10-11-2011, 08:39 AM
  5. [SOLVED] Maximum value by distinctive year
    By adelkam in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-15-2010, 05:54 PM
  6. Getting the Maximum Record # in Access
    By accessdanwi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-28-2008, 09:05 AM
  7. How do I sort for the maximum values in each year in excel?
    By The Wrightster in forum Excel General
    Replies: 4
    Last Post: 02-24-2005, 02:06 PM

Tags for this Thread

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