Private Sub CommandButton1_Click()
On Error Resume Next
Dim Var_Row As Double
Dim Var_Col As Double
Dim objDate1 As Date, _
objDate2 As Date
Dim Table1 As Range, _
Table2 As Range, _
Table3 As Range, _
FoundCell As Variant
'Find the column with header "Name" in and use its row and column
'values to specify the tables dynamically
With Sheet4
Set FoundCell = .Cells.Find("Name").Offset(1, 0)
Var_Row = .Cells(.Rows.Count, FoundCell.Column).End(xlUp).Row - FoundCell.Row + 1
Set Table1 = FoundCell.Resize(Var_Row)
End With 'Calculation
With Sheet2
Set FoundCell = .Cells.Find("Name").Offset(1, 0)
Var_Row = .Cells(.Rows.Count, FoundCell.Column).End(xlUp).Row - FoundCell.Row + 1
Set Table2 = FoundCell.Resize(Var_Row, 3)
Set Table3 = FoundCell.Offset(0, -1).Resize(Var_Row)
Set Table4 = FoundCell.Resize(Var_Row)
End With 'Master
'First Cell in the range
Var_Row = Sheet4.Range("H2").Row
Var_Col = Sheet4.Range("H2").Column
'Copy balance from Master to Calculation sheet for computation
For Each C1 In Table1
NameVal = C1
If Sheet4.Cells(Var_Row, Var_Col - 3).Value = "TMIL" Then
Sheet4.Cells(Var_Row, Var_Col) = Application.WorksheetFunction.VLookup(C1, Table2, 3, False)
oldVal = Application.WorksheetFunction.VLookup(C1, Table2, 3, False)
objDate2 = Application.WorksheetFunction.Index(Table3, Application.WorksheetFunction.Match(C1, Table4, 0), 1)
objDate1 = Sheet4.Cells(Var_Row, Var_Col - 7).Value
'Comparing date before updating the master sheet Date and Old_TMIL
'MsgBox "Date2: " & objDate2
'MsgBox "Date1: " & objDate1
If objDate1 > objDate2 Then
With Sheet2
.Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 4).Value = Sheet4.Cells(Var_Row, Var_Col).Value + Sheet4.Cells(Var_Row, Var_Col + 2).Value
.Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 1).Value = Sheet4.Cells(Var_Row, 1).Value
NewVal = .Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 4).Value
End With 'Master
End If
Call DoUpdate(oldVal, NewVal, NameVal)
End If
If Sheet4.Cells(Var_Row, Var_Col - 3).Value = "A/Leave" Then
Sheet4.Cells(Var_Row, Var_Col + 1) = Application.WorksheetFunction.VLookup(C1, Table2, 2, False)
oldVal = Application.WorksheetFunction.VLookup(C1, Table2, 2, False)
objDate2 = CDate(Application.WorksheetFunction.Index(Table3, Application.WorksheetFunction.Match(C1, Table4, 0), 1))
objDate1 = CDate(Sheet4.Cells(Var_Row, Var_Col - 7).Value)
'Comparing date before updating the master sheet Date and Old_Balance
If objDate1 > objDate2 Then
With Sheet2
.Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 3).Value = Sheet4.Cells(Var_Row, Var_Col + 1).Value + Sheet4.Cells(Var_Row, Var_Col + 3).Value
.Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 1).Value = Sheet4.Cells(Var_Row, 1).Value
NewVal = .Cells(Application.WorksheetFunction.Match(C1, Table4, 0) + 1, 3).Value
End With 'Master
End If
Call DoUpdate(oldVal, NewVal, NameVal)
End If
Var_Row = Var_Row + 1
Next C1
MsgBox "Done"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Column has the data is B column
If Target.Column = 2 Then
Application.EnableEvents = False
Cells(Target.Row, 1).Value = Date + Time
Application.EnableEvents = True
End If
End Sub
Sub DoUpdate(oldVal, NewVal, NameVal)
Dim Table5 As Range
Dim Var_Row As Double, Var_Col As Double
With Sheet5
Set FoundCell = .Cells.Find("Date").Offset(1, 0)
Var_Row = .Cells(.Rows.Count, FoundCell.Column).End(xlUp).Row - FoundCell.Row + 1
Set Table5 = FoundCell.Resize(Var_Row)
End With 'Calculation
Var_Row = Sheet5.Range("A2").Row
Var_Col = Sheet5.Range("A2").Column
MsgBox ("old Value: ") & oldVal
MsgBox ("new Value: ") & NewVal
If NewVal <> oldVal Then
Sheet5.Cells(Var_Row, Var_Col).Value = Date & " " & Time
Sheet5.Cells(Var_Row, Var_Col + 2).Value = Application.UserName
Sheet5.Cells(Var_Row, Var_Col + 3).Value = NameVal
Sheet5.Cells(Var_Row, Var_Col + 4).Value = "Value has changed from " & oldVal & " to" & NewVal
End If
Var_Row = Var_Row + 1
End Sub
[\code]