Private Sub copyButton_Click()
Dim fromAssignments As Object
Dim currAssignments As Object
Dim copyToMonthDates As Object
Dim copyToDateCounter As Integer
Dim endFound As Boolean
Dim rowCounter As Integer
Dim copyFromMonth As String
Dim copyToMonth As String
Dim numDates As Integer
copyFromMonth = copyFromMonthListBox.Value
copyToMonth = copyToMonthListBox.Value
' first check to see all items were selected. if not, error and send back.
If (resourcesListbox.Value = "") Or (copyFromMonthListBox.Value = "") Or (copyToMonthListBox.Value = "") Then
Let temp = MsgBox("Please make all selections before continuing.", vbOKOnly)
Else
Set fromAssignments = CreateObject("Scripting.Dictionary")
Set currAssignments = CreateObject("Scripting.Dictionary")
Set copyToMonthDates = CreateObject("Scripting.Dictionary")
copyToDateCounter = 1
' gather the existing projects and their mappings and store them before we clear and reset
endFound = False
rowCounter = 2
Do
' go through the Assignments tab and find all assignments for the resource in the copy from month
If Worksheets("Assignments").Cells(rowCounter, 1).Value <> "" Then
If (Worksheets("Assignments").Cells(rowCounter, 1).Value = resourcesListbox.Value) And (MonthName(Month(Worksheets("Assignments").Cells(rowCounter, 4).Value)) = copyFromMonthListBox.Value) Then
' this assignment is for the month we are copying from. check to see if we already added it to the array. if we did,
' add the number of hours to what is already there.
If fromAssignments.Exists(Worksheets("Assignments").Cells(rowCounter, 2).Value) Then
Let tempHours = fromAssignments.Item(Worksheets("Assignments").Cells(rowCounter, 2).Value)
tempHours = tempHours + Worksheets("Assignments").Cells(rowCounter, 3).Value
fromAssignments.Item(Worksheets("Assignments").Cells(rowCounter, 2).Value) = tempHours
Else
fromAssignments.Add Worksheets("Assignments").Cells(rowCounter, 2).Value, Worksheets("Assignments").Cells(rowCounter, 3).Value
End If
ElseIf (Worksheets("Assignments").Cells(rowCounter, 1).Value = resourcesListbox.Value) And (MonthName(Month(Worksheets("Assignments").Cells(rowCounter, 4).Value)) = copyToMonth) Then
' this assignment is for the current month. we want to keep a list of these so we don't accidentally, make multiple assignments
' for the same project on the same date via the copy.
If currAssignments.Exists(Worksheets("Assignments").Cells(rowCounter, 2).Value) Then
' do nothing since it is already an assignment
Else
currAssignments.Add Worksheets("Assignments").Cells(rowCounter, 2).Value, Worksheets("Assignments").Cells(rowCounter, 3).Value
End If
End If
Else
endFound = True
End If
rowCounter = rowCounter + 1
Loop Until endFound
' now that we have all the assignments for the month, add them to the chosen month to copy to.
' first get all the Monday dates of the month we are going to copy into
numDates = 0
inputDate = DateSerial(Year(Now()), 1, 1)
If MonthName(Month((inputDate - Weekday(inputDate, vbMonday) + 1))) = copyToMonth Then
copyToMonthDates.Add copyToDateCounter, (inputDate - Weekday(inputDate, vbMonday) + 1)
copyToDateCounter = copyToDateCounter + 1
numDates = numDates + 1
End If
For i = 2 To 52
inputDate = (DateAdd("d", 7, inputDate))
If MonthName(Month((inputDate - Weekday(inputDate, vbMonday) + 1))) = copyToMonth Then
copyToMonthDates.Add copyToDateCounter, (inputDate - Weekday(inputDate, vbMonday) + 1)
copyToDateCounter = copyToDateCounter + 1
numDates = numDates + 1
End If
Next i
For Each monthDate In copyToMonthDates.Keys()
For Each newAssignment In fromAssignments.Keys()
' add the new assignment to the list by first inserting a new row at the very top,
' but only add it if there isn't already an assignment on the same date
If Not currAssignments.Exists(newAssignment) Then
Worksheets("Assignments").Rows("2:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Assignments").Range("A2").Select
Worksheets("Assignments").Cells(2, 1).Value = resourcesListbox.Value
Worksheets("Assignments").Cells(2, 2).Value = newAssignment
Worksheets("Assignments").Cells(2, 3).Value = CInt((fromAssignments(newAssignment) / numDates) + 0.5)
Worksheets("Assignments").Cells(2, 4).Value = copyToMonthDates(monthDate)
End If
Next
Next
End If
' check to see if we should rebuild the dashboard
Let temp = MsgBox("Would you like to rebuild the dashboard?", vbYesNo)
If (temp = "6") Then
Unload Me
buildDashboard
Else
Unload Me
End If
' rebuild the project details now that new projects have been entered then referesh the pivot table
buildProjectDetails_Click
Worksheets("Project Dashboard").PivotTables("projectDashboardPvt").PivotCache.Refresh
End Sub