In your workbook, this macro will:
1) Add dates in column A of the result sheet if missing
2) Add Accounts to row 2 of the results sheet if missing
Option Explicit
Sub AddDataToDatabase()
Dim db As Worksheet
Dim data As Worksheet
Dim DateRng As Range
Dim NewDate As Range
Dim DateFND As Range
Dim AcntRNG As Range
Dim AcntCol As Long
Dim NR As Long
Dim NC As Long
Dim DateFmt As String
Set db = Sheets("Result")
Set data = Sheets("Data Input")
Set AcntRNG = db.Rows(2)
Set DateRng = data.Range("A:A").SpecialCells(xlConstants, xlNumbers)
NR = db.Range("A" & db.Rows.Count).End(xlUp).Row + 1
NC = db.Cells(2, db.Columns.Count).End(xlToLeft).Column + 1
DateFmt = DateRng.Cells(2).NumberFormat
On Error Resume Next
For Each NewDate In DateRng
Set DateFND = DateRng.Find(Format(NewDate, DateFmt), LookIn:=xlValues, LookAt:=xlWhole)
AcntCol = Application.WorksheetFunction.Match(NewDate.Offset(, 1), AcntRNG, 0)
If DateFND Is Nothing Then
db.Range("A" & NR) = NewDate
If AcntCol = 0 Then
db.Cells(2, NC) = NewDate.Offset(, 1)
db.Cells(NR, NC) = NewDate.Offset(, 2)
NC = NC + 1
Else
db.Cells(NR, AcntCol) = NewDate.Offset(, 2)
End If
NR = NR + 1
Else
If AcntCol = 0 Then
db.Cells(2, NC) = NewDate.Offset(, 1)
db.Cells(DateFND.Row, NC) = NewDate.Offset(, 2)
NC = NC + 1
Else
db.Cells(DateFND.Row, AcntCol) = NewDate.Offset(, 2)
End If
End If
AcntCol = 0
Set DateFND = Nothing
Next NewDate
End Sub
Bookmarks