+ Reply to Thread
Results 1 to 6 of 6

Finding Start date and End date for value using vba

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2018
    Location
    INDIA
    MS-Off Ver
    16
    Posts
    40

    Finding Start date and End date for value using vba

    I have an Excel sheet looks like below. I want the start date and end date for a task

    Data

    HTML Code: 



    Summarized to

    HTML Code: 

    Can you please help me
    Attached Files Attached Files
    Last edited by AV114; 09-20-2018 at 02:58 AM. Reason: attaching the file

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Finding Start date and End date for value using vba

    Are you sure you want to use VBA? You can achieve these values with formulas:

    B18 and C18 (Enter with Ctrl+Shift+Enter):
    =MIN(IF($A$2:$A$13=$A18,$B$2:$B$13))
    =MAX(IF($A$2:$A$13=$A18,$B$2:$B$13))
    D18:
    =SUMIF($A$2:$A$13,$A18,$C$2:$C$13)
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Finding Start date and End date for value using vba

    Try this:
    Sub zz()
    Dim d As Object, k, t, f$
    Set d = CreateObject("scripting.dictionary")
    f = [b2].NumberFormat
    ar = [a1].CurrentRegion.Value
    For i = 2 To UBound(ar)
        k = ar(i, 1)
        If Not d.exists(k) Then
            d(k) = Array(ar(i, 2), ar(i, 2), ar(i, 3))
        Else
            t = d(k)
            t(2) = t(2) + ar(i, 3)
            t(1) = IIf(t(1) > ar(i, 2), t(1), ar(i, 2))
            d(k) = t
        End If
    Next
    [a18].Resize(d.Count, 4).Borders.Value = 1
    [a18].Resize(d.Count, 1) = Application.Transpose(d.keys)
    [b18].Resize(d.Count, 2).NumberFormat = f
    [b18].Resize(d.Count, 3) = Application.Transpose(Application.Transpose(d.items))
    End Sub

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

    Re: Finding Start date and End date for value using vba

    @ikboy
    Great code. To adjust the output for dates it is better to use CLng
    d(k) = Array(CLng(ar(i, 2)), CLng(ar(i, 2)), ar(i, 3))
    t(1) = IIf(t(1) > CLng(ar(i, 2)), t(1), CLng(ar(i, 2)))
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Finding Start date and End date for value using vba

    YasserKhalil ,
    You right, Clng is better.
    Thx
    ikboy

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Finding Start date and End date for value using vba

    You can do it with a Pivot Table

    Sub Summary_PT()
        
        Dim rng As Range
        
        Set rng = Range("A1").CurrentRegion
        
        ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
                                       SourceData:=rng).CreatePivotTable _
                                       TableDestination:=Range("E2")
        With ActiveSheet.PivotTables(ActiveSheet.PivotTables.Count)
            .ColumnGrand = False
            .PageFieldOrder = xlOverThenDown
            .RowGrand = False
            .AddFields RowFields:=Array("StockCode", "Data")
            With .PivotFields("TrnDate")
                .Orientation = xlDataField
                .Caption = "Start Date"
                .Position = 1
                .Function = xlMin
                .NumberFormat = "m/d/yyyy"
            End With
            With .PivotFields("TrnDate")
                .Orientation = xlDataField
                .Caption = "End Date"
                .Position = 2
                .Function = xlMax
                .NumberFormat = "m/d/yyyy"
            End With
            With .PivotFields("TrnQuantity")
                .Orientation = xlDataField
                .Caption = " Total TrnQuantity"
                .Position = 3
            End With
            With .DataPivotField
                .Orientation = xlColumnField
                .Position = 1
            End With
        End With
        Application.CommandBars("PivotTable").Visible = False
        ActiveWorkbook.ShowPivotTableFieldList = False
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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] subset of range defined by start date and date date in dedicated cells
    By Pragmaticite in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-23-2015, 05:30 PM
  2. Replies: 2
    Last Post: 08-09-2014, 12:40 PM
  3. Counting if data falls on date between Start Date, End Date or Possible End Date
    By JessHasQuestions in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 07-26-2014, 08:01 AM
  4. [SOLVED] Finding start date and end date
    By antb2000 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-04-2014, 10:50 PM
  5. Replies: 1
    Last Post: 10-02-2012, 02:42 PM
  6. Replies: 2
    Last Post: 01-04-2012, 09:15 AM
  7. Replies: 7
    Last Post: 11-16-2008, 05:48 PM

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