Sub UpdatePeopleHours()
Dim firstRowSrc, lastRowSrc, firstRowDest, lastRowDest, firstColumnDest, lastColumnDest, beforeFrenchTimeInterval, beginningBeforeFrenchTimeInterval, endingBeforeFrenchTimeInterval, columnOfPersonDest, columnOfSAndP, columnOfLQ, columnOfProject, currentRowSrc, currentPersonSkill, currentColumnDest, currentRowDest As Long
Dim wsSrc, wsDest1, wsDest2 As Worksheet
Dim frenchHour, currentPersonSrc As String
Set wsSrc = Workbooks("the_larger_file.xlsm").Sheets(1)
Set wsDest1 = ThisWorkbook.Sheets(1)
Set wsDest2 = ThisWorkbook.Sheets(2)
firstRowSrc = wsDest2.Range("B3").Value
lastRowSrc = wsSrc.Range("A:A").Find("*", Range("A1"), SearchDirection:=xlPrevious).Row
lastColumnDest = wsDest1.Cells(1, 1).End(xlToLeft).Column
For currentRowSrc = firstRowSrc To lastRowSrc
currentPersonSrc = wsSrc.Cells(currentRowSrc, 11).Value
currentPersonSkill = wsSrc.Cells(currentRowSrc, 5).Value
columnOfPersonDest = -1
For currentColumnDest = 11 To lastColumnDest 'This for loop finds out which column (the row is constant) the name of a person already added is at in dest ws as well as columns for "S&P", "Loto-Quebec", and "Project"
If wsDest1.Cells(1, currentColumnDest) = wsSrc.Cells(currentRowSrc, 11) Then
columnOfPersonDest = currentColumnDest
End If
If wsDest1.Cells(1, currentColumnDest) = "S&P" Then
columnOfSAndP = currentColumnDest
End If
If wsDest1.Cells(1, currentColumnDest) = "Loto Quebec" Then
columnOfLQ = currentColumnDest
End If
If wsDest1.Cells(1, currentColumnDest) = "Project" Then
columnOfProject = currentColumnDest
End If
Next currentColumnDest
If columnOfPersonDest = -1 Then 'In other words, if the person in the src ws was not found in the dest ws
If wsSrc.Cells(currentRowSrc, 5) = "LQ" Then
wsDest1.Range(wsDest1.Cell(1, columnOfLQ + 1), wsDest1.Cell(1, lastColumnDest)).Copy wsDest1.Range(wsDest1.Cell(1, columnOfLQ + 2), wsDest1.Cell(1, lastColumnDest + 1)) 'shift all columns from beyond columnOfLQ by 1 column and place the name of the person from wsSrc.Cell(currentRowSrc, 11) into wsDest1.Cell(currentRowDest, columnOfLQ+1)
lastColumnDest = lastColumnDest + 1 'Increment by one since I'm adding a column when shifting and storing new person from previous line
Else 'if it's S&P or anything other than LQ
wsDest1.Range(wsDest1.Cells(1, columnOfSAndP + 1), wsDest1.Cells(1, lastColumnDest)).Copy wsDest1.Range(wsDest1.Cells(1, columnOfSAndP + 2), wsDest1.Cells(1, lastColumnDest + 1)) 'shift all columns from beyond columnOfSAndP by 1 column and place the name of the person from wsSrc.Cells(currentRowSrc, 11) into wsDest1.Cells(currentRowDest, columnOfSAndP+1)
lastColumnDest = lastColumnDest + 1 'Increment by one since I'm adding a column when shifting and storing new person from previous line
End If
End If
firstRowDest = wsDest2.Range("B4").Value
For currentRowDest = firstRowDest To lastRowDest
beforeFrenchTimeInterval = 600
frenchHour = Left(Format(beforeFrenchTimeInterval, "0000"), 2) & ":" & Mid(Format(beforeFrenchTimeInterval, "0000"), 3)
If CDbl(beforeFrenchTimeInterval / 100) = Round(CDbl(beforeFrenchTimeInterval / 100)) Then
beforeFrenchTimeInterval = beforeFrenchTimeInterval + 30
Else
beforeFrenchTimeInterval = beforeFrenchTimeInterval - 30 + 100
End If
wsDest1.Cells(currentRowDest, 1) = wsSrc.Cells(currentRowSrc, 1) 'write the date taken from src: currentRowSrc and column A aka 1
wsDest1.Cells(currentRowDest, 2) = wsSrc.Cells(currentRowSrc, 2) 'write the day in currentRowDest and column B aka 2
wsDest1.Cells(currentRowDest, 3) = frenchHour 'write the time in currentRowDest and column C aka 3
If wsSrc.Cell(currentRowSrc, 12).Value <> 0 Or wsSrc.Cell(currentRowSrc, 20).Value <> 0 Then
If wsSrc.Cell(currentRowSrc, 12).Value <> 0 And wsSrc.Cell(currentRowSrc, 20).Value <> 0 Then
If wsSrc.Cell(currentRowSrc, 12).Value > wsSrc.Cell(currentRowSrc, 20).Value Then
beginningBeforeFrenchTimeInterval = wsSrc.Cell(currentRowSrc, 20).Value
Else
beginningBeforeFrenchTimeInterval = wsSrc.Cell(currentRowSrc, 12).Value
End If
End If
End If
If wsSrc.Cell(currentRowSrc, 13).Value <> 0 Or wsSrc.Cell(currentRowSrc, 21).Value <> 0 Then
If wsSrc.Cell(currentRowSrc, 13).Value <> 0 And wsSrc.Cell(currentRowSrc, 21).Value <> 0 Then
If wsSrc.Cell(currentRowSrc, 13).Value > wsSrc.Cell(currentRowSrc, 21).Value Then
beginningBeforeFrenchTimeInterval = wsSrc.Cell(currentRowSrc, 13).Value
Else
beginningBeforeFrenchTimeInterval = wsSrc.Cell(currentRowSrc, 21).Value
End If
End If
End If
If beforeFrenchTimeInterval >= beginningBeforeFrenchTimeInterval Or beforeFrenchTimeInterval <= endingBeforeFrenchTimeInterval Then
wsDest1.Cell(currentRowDest, columnOfPersonDest) = "W"
End If
Next currentRowDest
beforeFrenchTimeInterval = 600
Next currentRowSrc
wsDest2.Range("B3").Value = lastRowSrc + 1 'set the data in sheet 2 of dest file beginning firstRowSrc of next time this macro is run to be 1 more than the current lastRowSrc
End Sub
Like I said, when I run it, it hangs indefinitely however, when I click to close it and then tell Windows to restart the program, it says:
wsDest1.Range(wsDest1.Cells(1, columnOfSAndP + 1), wsDest1.Cells(1, lastColumnDest)).Copy wsDest1.Range(wsDest1.Cells(1, columnOfSAndP + 2), wsDest1.Cells(1, lastColumnDest + 1)) 'shift all columns from beyond columnOfSAndP by 1 column and place the name of the person from wsSrc.Cells(currentRowSrc, 11) into wsDest1.Cells(currentRowDest, columnOfSAndP+1)
Is there something wrong at all or does VBA take
Bookmarks