Hello,
I have a set of data that keeps a rolling 12 months (currently Feb '19 - Jan '20). I have an InputBox asking for a start date of an employee and, for all rows of data that exist for that employee before the start date, I need a "-1" placed in column S. I have written the code to do that, and have pasted it below.
I have another condition, however, that I need addressed and I CANNOT figure out how to execute! If a month exists in the data set, but the user doesn't have a row of data for that month, then I need to create a "placeholder" row with the username, that month and year, and a "-1" in column S.
The end goal is that every month that exists in the data set before the start date of the employee should have at least one line of data (even if it's just a "placeholder" line) with the username in D and the "-1" in S. Again, if a line of data already exists, I have the -1 added, but I need a way to add rows for the months that don't have data.
Some additional information: In the data set, I have the year in column A, the month in Column B, and the full date of the first of the month (mm/01/yyyy) in column T.
Thanks so much for any help you can offer with this!! It's driving me CRAZY! lol
Kate
I have a set of data that keeps a rolling 12 months (currently Feb '19 - Jan '20). I have an InputBox asking for a start date of an employee and, for all rows of data that exist for that employee before the start date, I need a "-1" placed in column S. I have written the code to do that, and have pasted it below.
I have another condition, however, that I need addressed and I CANNOT figure out how to execute! If a month exists in the data set, but the user doesn't have a row of data for that month, then I need to create a "placeholder" row with the username, that month and year, and a "-1" in column S.
The end goal is that every month that exists in the data set before the start date of the employee should have at least one line of data (even if it's just a "placeholder" line) with the username in D and the "-1" in S. Again, if a line of data already exists, I have the -1 added, but I need a way to add rows for the months that don't have data.
Some additional information: In the data set, I have the year in column A, the month in Column B, and the full date of the first of the month (mm/01/yyyy) in column T.
Thanks so much for any help you can offer with this!! It's driving me CRAZY! lol
Kate
VBA Code:
Dim month As Integer
Dim year As Integer
Dim r As Integer
Dim lastlog As Long
Dim name As String
Dim note As String
Dim maxdate As Date
UserID = InputBox("Enter the username you'd like to add a start date for.", vbOKCancel)
If UserID = vbNullString Then Exit Sub
DateID = InputBox("Enter the start date of the employee.", vbOKCancel)
If DateID = vbNullString Then Exit Sub
ThisWorkbook.Sheets("Training").Range("E2").Value = UserID
ThisWorkbook.Sheets("Training").Range("F2").Value = DateID
year = ThisWorkbook.Sheets("Training").Range("F3").Value
month = ThisWorkbook.Sheets("Training").Range("E3").Value
firstdate = ThisWorkbook.Sheets("Training").Range("G2").Value
'Check for username in data set ("Overall User Data" and then "New Data Add") - alert if there is no data for this user.
With ThisWorkbook.Sheets("Overall User Data").Columns(4)
Set Fnd = .Find(UserID, , , xlWhole, , , False, , False)
End With
If Fnd Is Nothing Then
ans = MsgBox("This user is not found in the dataset. Please add them as a new user first, then adjust the start date.")
End If
'Change all existing rows of dates from blank to "-1" in S
For r = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count To 1 Step -1
If ThisWorkbook.Sheets("Overall User Data").Cells(r, "A") = year Then
If ThisWorkbook.Sheets("Overall User Data").Cells(r, "B") < month Then
If ThisWorkbook.Sheets("Overall User Data").Cells(r, "D") = UserID Then
ThisWorkbook.Sheets("Overall User Data").Cells(r, "S").Value = "-1"
End If
End If
End If
Next
For r = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count To 1 Step -1
If ThisWorkbook.Sheets("Overall User Data").Cells(r, "A") < year Then
If ThisWorkbook.Sheets("Overall User Data").Cells(r, "D") = UserID Then
ThisWorkbook.Sheets("Overall User Data").Cells(r, "S").Value = "-1"
End If
End If
Next
'Add a "placeholder" row for any month/year before the start date that doesn't already have data with a "-1" in S.