+ Reply to Thread
Results 1 to 19 of 19

Convert AGGREGATE formula to VBA 3

  1. #1
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Convert AGGREGATE formula to VBA 3

    Hi All

    Continuing from my previous 2 threads:

    http://https://www.excelforum.com/ex...ml#post5843551
    https://www.excelforum.com/excel-pro...ml#post5842951

    Now, I am required to display the First Sales Date and Last Sales Date in respective sheets.

    Need your help again, thank you!

    Sincerely
    Joseph
    Attached Files Attached Files

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

    Re: Convert AGGREGATE formula to VBA 3

    Again, flexibility first.
    Assuming Sales Data is sorted in ascending order by PERIOD column.
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Thank you jindon for once again coming to my rescue.

    Can I request to modify the code to allow for other columns (not fixed) in the "Sales Data" table?

    Thank you.
    Attached Files Attached Files

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

    Re: Convert AGGREGATE formula to VBA 3

    change to
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Thank you jindon, works perfectly!

    2 mistakes made by me: (1) there is no "Company Code" in Sales Data sheet, and (2) it's called "Company Name" (not "Company Code") in the Customer Master sheet.

    Can you help me to modify the code please? Thank you so much!

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

    Re: Convert AGGREGATE formula to VBA 3

    try change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Thank you for your prompt response, jindon! The "Company First & Last Sales Dates" are not showing, possibly because "Company Name" has been removed from Sales Data sheet.
    Attached Files Attached Files
    Last edited by josephteh; 07-03-2023 at 07:22 AM. Reason: Upload workbook

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

    Re: Convert AGGREGATE formula to VBA 3

    And what do you expect for Company First/Last Sales?

  9. #9
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,661

    Re: Convert AGGREGATE formula to VBA 3

    I'm not sure about your exact requirement, but with the Company name in the Customer Master sheet, how can we retrieve the start date and end date when there are two types of dates in the sheet? However, I assume that the Company will retrieve data from CUS (the start date and end date of CUS). Since a company can have multiple CUS, it will take the smallest start date and the largest end date of its CUS.
    My approach is as follows:
    I will use three dictionaries to store the lists of Cus, Stock, and Com, with their keys being a concatenation of the start date and end date in a string, linked by '|'. I will iterate through the lists, comparing the start date and end date with the dates in the list to filter out the smallest start date and the largest end date.
    PHP Code: 
    Sub FirstLastDate()
    Dim lr&, i&, rngres()
    Dim dCust As ObjectdStock As ObjectdCom As Object
    Dim sp
    sp1Mn As DoubleMx As Double
    Set dCust 
    CreateObject("Scripting.Dictionary")
    Set dStock CreateObject("Scripting.Dictionary")
    Set dCom CreateObject("Scripting.Dictionary")
    With Sheets("Sales Data")
        
    lr = .Cells(Rows.Count1).End(xlUp).Row
        rng 
    = .Range("A2:J" lr).Value2
        
    For 1 To UBound(rng)
        
            
    ' generate Customers data
            If Not dCust.exists(rng(i, 4)) Then
                dCust.Add rng(i, 4), rng(i, 1) & "|" & rng(i, 1) ' 
    get the first pair of date
            
    Else ' from 2nd occurence, to get smaller date (Mn) and larger date (Mx)
                sp = Split(dCust(rng(i, 4)), "|")
                With WorksheetFunction
                    Mn = .Min(rng(i, 1), sp(0))
                    Mx = .Max(rng(i, 1), sp(1))
                End With
                dCust(rng(i, 4)) = Mn & "|" & Mx
            End If
            
            ' 
    generate Stock data
            
    If Not dStock.exists(rng(i10)) Then
                dStock
    .Add rng(i10), rng(i1) & "|" rng(i1' get the first pair of date
            Else ' 
    from 2nd occurenceto get smaller date (Mn) and larger date (Mx)
                
    sp Split(dStock(rng(i10)), "|")
                
    With WorksheetFunction
                    Mn 
    = .Min(rng(i1), sp(0))
                    
    Mx = .Max(rng(i1), sp(1))
                
    End With
                dStock
    (rng(i10)) = Mn "|" Mx
            End 
    If
        
    Next
    End With
    With Sheets
    ("Customer Master")
        
    lr = .Cells(Rows.Count1).End(xlUp).Row
        rng 
    = .Range("A2:D" lr).Value2
        ReDim res
    (1 To UBound(rng), 1 To 4)
        For 
    1 To UBound(rng)
            
    sp Split(dCust(rng(i1)), "|")
            
            
    ' Get first & last date for CUS
            res(i, 1) = sp(0): res(i, 2) = sp(1)
            '
    working with Company
            
    If Not dCom.exists(rng(i4)) Then
                dCom
    .Add rng(i4), dCust(rng(i1)) ' get the first pair of date
            Else ' 
    from 2nd occurenceto get smaller date (Mn) and larger date (Mx)
                
    sp1 Split(dCom(rng(i4)), "|")
                
    With WorksheetFunction
                    Mn 
    = .Min(sp1(0), sp(0))
                    
    Mx = .Max(sp1(1), sp(1))
                
    End With
                dCom
    (rng(i4)) = Mn "|" Mx
            End 
    If
        
    Next
        
    For 1 To UBound(rng)
            
    sp Split(dCom(rng(i4)), "|")
            
    res(i3) = sp(0): res(i4) = sp(1)
        
    Next
    .Range("G2:J100000").ClearContents
    .Range("G2").Resize(UBound(res), 4).Value res
    End With
    With Sheets
    ("Stock Master")
        
    lr = .Cells(Rows.Count1).End(xlUp).Row
        rng 
    = .Range("A2:A" lr).Value
        ReDim res
    (1 To UBound(rng), 1 To 2)
        For 
    1 To UBound(rng)
            
    sp Split(dStock(rng(i1)), "|")
            
    res(i1) = sp(0): res(i2) = sp(1)
        
    Next
    .Range("E2:F100000").ClearContents
    .Range("E2").Resize(UBound(res), 2).Value res
    End With
    End Sub 
    Attached Files Attached Files
    Quang PT

  10. #10
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Quote Originally Posted by jindon View Post
    And what do you expect for Company First/Last Sales?
    Thank you jindon for checking back. As Quang has correctly pointed out in post #9, the sales dates for the "Company Name" are derived from the sales dates of "Cust Code" in the Customer Master sheet.

    "Cust Codes" are actually the branch codes of the Company.

  11. #11
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,661

    Re: Convert AGGREGATE formula to VBA 3

    Quote Originally Posted by josephteh View Post
    Thank you jindon for checking back. As Quang has correctly pointed out in post #9, the sales dates for the "Company Name" are derived from the sales dates of "Cust Code" in the Customer Master sheet.
    So, does the code in #9 work?

  12. #12
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Thank you Quang PT, your codes seemed to be working well.

  13. #13
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,661

    Re: Convert AGGREGATE formula to VBA 3

    Quote Originally Posted by josephteh View Post
    Thank you Quang PT, your codes seemed to be working well.
    Although the code is functional, I am not very confident about its speed with large datasets. I hope that someone will join in and provide the most optimized code for your situation.
    Have you tested it with large data yet?

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

    Re: Convert AGGREGATE formula to VBA 3

    If other code is working, go for it, don't bother.

  15. #15
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Hi jindon, your code is more flexible.

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

    Re: Convert AGGREGATE formula to VBA 3

    Then you need to tell me that if that is the only one irregular case or if you have more, need to know all of them.

  17. #17
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Hi jindon. Confirmed. No more further cases. Thank you.

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

    Re: Convert AGGREGATE formula to VBA 3

    See if this is how you wanted.
    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2019
    Posts
    4,542

    Re: Convert AGGREGATE formula to VBA 3

    Working perfectly! Thank you jindon & Quang. Rep given!

+ 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 AGGREGATE formula to VBA
    By josephteh in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 06-30-2023, 03:10 AM
  2. [SOLVED] Convert AGGREGATE formula to VBA 2
    By josephteh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2023, 01:02 AM
  3. [SOLVED] Index aggregate formula help
    By frencett in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-02-2021, 08:26 AM
  4. [SOLVED] Aggregate Max versus Aggregate Large
    By ChemistB in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-16-2020, 03:38 PM
  5. Convert INDEX MATCH AGGREGATE FORMULA to VBA code
    By samuelkmh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2019, 03:28 AM
  6. Convert INDEX, AGGREGATE to Macro
    By VirenS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-25-2016, 02:08 PM
  7. aggregate Formula for Excel
    By GSO in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-28-2006, 10:45 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