所以这是我第一次尝试自己的代码。到目前为止,我总能找到一些让我朝着正确的方向前进的东西,但我被困在这里。我对下面的代码进行了评论,以显示我正在尝试做什么,但它失败了。任何帮助当然值得赞赏。
Sub Update_Hours()
Dim ws1, ws2 As Worksheet
Dim ws1_employee, ws2_employee As String
Dim ws1_lastrow, ws2_lastrow, currenthours, latesthours, newhours As Long
Dim ws1_counter, ws2_counter As Integer
Set ws1 = ActiveWorkbook.Sheets("Current Hours")
Set ws2 = ActiveWorkbook.Sheets("Latest Hours")
With ws1
ws1_lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Find the last row in the Current Hours sheet
End With
With ws2
ws1_lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Find the last row in the Latest Hours sheet
End With
For ws1_counter = 2 To ws1_lastrow 'Loop through all values in column A on Current Hours sheet
ws1_employee = ws1.Cells("A" & ws1_counter).Value
Set currenthours = ws1.Cells("F" & ws1_counter).Value
For ws2_counter = 2 To ws2_lastrow 'Loop through all values in column A on Latest Hours sheet
ws2_employee = ws2.Cells("A" & ws2_counter).Value
If ws1_employee = ws2_employee Then 'Check for a match between the two employee names
latesthours = ws2.Cells("C" & ws2_counter).Value
newhours = latesthours + currenthours
ws1.Cells("F" & ws1_counter).Value = newhours 'Update the cell in the Current Hours sheet with the new total hours
End If
Next
Next
End Sub