See if the following does what you want. This macro **requires** your data
to be sorted by account number first and then by date second; and it also
**requires** that your source data start **on or after** Row 2 (whether you
have a header row or not). Before running the program, set the values in the
Const statements to those that match your actual setup (Account and DateCol
are column letters where your account numbers and transaction dates are
located).
Sub CreateReport()
' Set the Const(ant) values to reflect your data
Const SourceDataStartRow As Long = 2
Const ReportDataStartRow As Long = 2
Const DaysDifferential As Long = 30
Const Account As String = "A"
Const DateCol As String = "C"
Const SourceSheet As String = "Sheet3"
Dim X As Long
Dim Z As Long
Dim Index As Long
Dim LastRow As Long
Dim ReportRow As Long
Dim NewAccount As Long
Dim ReportSheet As Worksheet
Dim TestValue As String
Dim Hits() As String
With Worksheets(SourceSheet)
LastRow = .Cells(Rows.Count, DateCol).End(xlUp).Row
ReDim Hits(0 To LastRow)
Hits(0) = "X"
NewAccount = SourceDataStartRow
For X = SourceDataStartRow + 1 To LastRow
If TestValue <> .Cells(X, Account).Value Then
If .Cells(X, Account).Value <> .Cells(X - 1, Account).Value Then
NewAccount = X
End If
If .Cells(X, Account).Value <> Split(Hits(Index), Chr(1))(0) Then
If .Cells(X, Account).Value = .Cells(X - 1, Account).Value And _
.Cells(X, DateCol).Value - .Cells(X - 1, _
DateCol).Value < DaysDifferential Then
Index = Index + 1
TestValue = .Cells(X, Account).Value
Hits(Index) = .Cells(X, Account).Value & Chr(1) & NewAccount
End If
End If
End If
Next
ReDim Preserve Hits(0 To Index)
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Set ReportSheet = Worksheets(Worksheets.Count)
ReportSheet.Name = "Report (" & Format(Now, _
"dd-mmm-yyyy hh\hmm\mss\s") & ")"
If ReportDataStartRow > 1 Then
.Rows(ReportDataStartRow).Offset(-1).Copy ReportSheet.Rows(1)
End If
ReportRow = ReportDataStartRow
For X = 1 To Index
Z = Split(Hits(X), Chr(1))(1)
Do While .Cells(Z, Account).Value Like Split(Hits(X), Chr(1))(0)
.Cells(Z, Account).EntireRow.Copy ReportSheet.Cells(ReportRow, "A")
Z = Z + 1
ReportRow = ReportRow + 1
Loop
Next
End With
End Sub
Rick