October 12, 2017

Automate Deleting Duplicate Dates Insert Rows Make Date Entries

How to automatically remove duplicate date entries, automate multiple rows insertion and automate date entries in the inserted rows. Often we need to remove duplicate entries against duplicate dates and make missing entries between the last entered date and the end of the month date. This post explains how to determine the last day of the month. Then the macro detects the last date of entry. The VBA code next automatically inserts the required number of rows for further date and data entries. Watch the video below:

Watch this video on YouTube.

Here’s the complete VBA code which you can alter or optimize for your own automated solution:

Sub findDuplicates()
Dim i As Long, lastRow As Long
lastRow = Sheets(“Sheet1”).Range(“A” & Rows.count).End(xlUp).Row
Dim employeeno As Long
Dim lastDayofMonth As Integer

Cells(2, 3) = “=DATE(YEAR(RC[-1]),MONTH(RC[-1])+1,0)”
lastDayofMonth = Day(Cells(2, 3))
MsgBox “The last day of date is ” & lastDayofMonth

employeeno = InputBox(“Enter employee number”)

For i = 2 To lastRow

If Cells(i, 1) = employeeno And Cells(i, 2) = Cells(i + 1, 2) Then
Cells(i + 1, 2).EntireRow.Delete
End If

Next i


For i = 2 To lastRow
If Cells(i, 1) = employeeno Then
Cells(i + 1, 1).Select
End If

Next i

Dim myvalue As Long

myvalue = ActiveCell.Row

MsgBox “myvalue is ” & myvalue

dayofLastDateEntry = Day(Cells(myvalue – 1, 2))

Rows(ActiveCell.Row & “:” & ActiveCell.Row + (lastDayofMonth – dayofLastDateEntry) – 1).Insert shift:=xlDown

For i = myvalue To ((myvalue) + (lastDayofMonth – dayofLastDateEntry – 1))
Cells(i, 1) = employeeno
Cells(i, 2) = Cells(i – 1, 2).Value + 1
Next i

End Sub

Further reading:

Insert multiple rows using VBA in Excel