Link to home
Start Free TrialLog in
Avatar of Ryan Simmons
Ryan Simmons

asked on

VBA: Find the number of days that occured between dates in records. Example Attached

In the attached worksheet I have two tabs. The first tab is example data I am working with. The second tab is the result I am looking for. The data lives in a MS Access 2016 database.

I worked with a expert on this forum seeking a query solution. However he recommended VBA as a solution.

What I need to do is to loop the sorted table in VBA and then, record by record, find the count of days between each originatordn and the previous record.
Then append the record to a new table.

The goal is to calculate the days since last call for every originatordn.

Example Result:
originatordn      Date      Month      Year      DaysSinceLastCall
2053843380      01-Jan-17      1      2017      0
2053843380      03-Jan-17      1      2017      2
2053843380      05-Jan-17      1      2017      2
2053843380      08-Jan-17      1      2017      3
2053843380      18-Jan-17      1      2017      10
2053843380      28-Jan-17      1      2017      10
Example_DaysSinceLastCall.xlsx
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Sub macro()
Range("D1").Copy Range("E1")
Range("E1").FormulaR1C1 = "DaysSinceLastCall"
Range("E1").EntireColumn.AutoFit
For Each c In Range(Range("A2"), Range("A" & Rows.Count))
    If c.Offset(-1) = c Then
        c.Offset(, 4) = c.Offset(0, 1) - c.Offset(-1, 1)
    Else
        c.Offset(, 4) = 0
    End If
Next
End Sub

Open in new window

Regards
Avatar of Ryan Simmons

ASKER

That works... ty!
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I'll suggest a combo of query and VBA solution:
Option Explicit

Public Sub getData()
    Const adOpenStatic As Integer = 3
    
    Dim wb As Excel.Workbook
    Set wb = ThisWorkbook

    Dim cn As Object        '// ADODB.Connection
    Set cn = CreateObject("ADODB.Connection")
    cn.ConnectionString = getConnectionString(wb.FullName)
    cn.Open
    
    Dim SQL As String
    SQL = vbNullString
    SQL = SQL & "SELECT originatordn," & vbCrLf
    SQL = SQL & "       MAX([Date]) AS [Date]," & vbCrLf
    SQL = SQL & "       MAX([Month]) AS [Month]," & vbCrLf
    SQL = SQL & "       MAX([year]) AS [Year]" & vbCrLf
    SQL = SQL & "FROM [Example Data$]" & vbCrLf
    SQL = SQL & "GROUP BY originatordn;"
    
    Dim rs As Object        '// ADODB.Recordset
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open SQL, cn, adOpenStatic
    
    Dim ws As Excel.Worksheet
    Set ws = wb.Worksheets("Example Result")
    copyHeaders rs, ws
    doFormatting ws
    
    Dim rng As Excel.Range
    Set rng = ws.Range("A2")
    rng.CopyFromRecordset rs
    
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    
    computedays ws
    Set ws = Nothing
    Set wb = Nothing
End Sub

Private Function getConnectionString(ByVal path As String) As String
    getConnectionString = vbNullString
    getConnectionString = getConnectionString & "Provider=Microsoft.ACE.OLEDB.12.0;"
    getConnectionString = getConnectionString & "Data Source=" & path & ";"
    getConnectionString = getConnectionString & "Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
End Function

Private Sub copyHeaders(ByRef rs As ADODB.Recordset, ByRef ws As Excel.Worksheet)
    Dim rng As Excel.Range
    Set rng = ws.Range("A1")
    
    Dim field As ADODB.field
    For Each field In rs.Fields
        rng.Value = field.Name
        Set rng = rng.Offset(columnoffset:=1)
    Next
    rng.Value = "DaysSinceLastCall"
    Set rng = Nothing
End Sub

Private Sub computedays(ByRef ws As Excel.Worksheet)
    Dim rng As Excel.Range
    
    Set rng = ws.Range("D2")
    While rng.Value
        rng.Offset(columnoffset:=1).FormulaR1C1 = "=DAYS(NOW(),RC[-3])"
        Set rng = rng.Offset(rowoffset:=1)
    Wend
    Set rng = Nothing
End Sub

Private Function doFormatting(ByRef ws As Excel.Worksheet)
    Dim rng As Excel.Range
    Set rng = ws.Range("B:B")
    
    rng.NumberFormat = "m/d/yyyy"
    Set rng = Nothing
End Function

Open in new window

PS: I wrote this within a workbook, it is easilly transferable to Ms Access.
don't forget to late bind excel library.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
scratch my answer, missed Something.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial