I have two sheets. TimeSheetEntry (TSE) and CLNTINFO (CI). TSE has daily time sheet entries including leave time hours taken using the codes 186 and 189. CI has the balance of leave hours available in Col G.. I am needing to update the balance in CI from the weeks entries on TSE. The first sub below works, but if and only if the client number value from TSE happens to match the current value in Col A of CI. Not realistic. I tried using .Find in the 2nd sub below but ran into Range and Type Mismatch issues. How can get my records on CI to be updated? Thanks in advance.
Code:
Sub UpdateLeaveHours()
Application.StatusBar = "Updating Leave Time Balance"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim LeaveHours As Single
Dim ClientNumber As Single
'setting CLNTINFO sheet to A1
Worksheets("CLNTINFO").Select
Range("A1").Select 'client number
Sheets("TimeSheetEntry").Select
'sort by job # which is Col B, then by client# Col A
SortByJobThenClient 'separate proc that sorts data on TimeSheetEntry sheet.
Range("B2").Select 'col B has job #'s. headers in col's.
TimeSheetEntry: 'come back for next client entry
Sheets("TimeSheetEntry").Select
Do 'should be able to us a better method than a DO loop for this proc.
With ActiveCell 'AC is B2 on TimeSheetEntry sheet
If ActiveCell.Value = 186 Or ActiveCell.Value = 189 Then '
LeaveHours = ActiveCell.Offset(0, 1).Value
ClientNumber = ActiveCell.Offset(0, -1).Value 'client # in Col A
'info is in variables LeaveHours and ClientInfo.
ActiveCell.Offset(1, 0).Select 'still on TimeSheetEntry sheet. _
Moving down a row for next time thru loop
'Go to CLNTINFO sheet now.
Worksheets("CLNTINFO").Select
'following works if and only if the value in ClientNumber = the value _
in the activecell on the CLNTINFO sheet. not gonna happen in the real world.
'Need to use Find or Match to find the correct ClientNumber on the CLNTINFO sheet?
Do
With ActiveCell 'A1 on CLNTINFO sheet
If ActiveCell.Value = ClientNumber Then
ActiveCell.Offset(0, 6).Value = ActiveCell.Offset(0, 6).Value - LeaveHours
End If
ActiveCell.Offset(1, 0).Select 'still on CLNTINFO sheet
End With
GoTo TimeSheetEntry
Loop Until ActiveCell.Value = ""
'Sheets("TimeSheetEntry").Select 'go back for next client with leave time
End If
ActiveCell.Offset(1, 0).Select
End With
Loop Until ActiveCell > 189 'anything higher is not leave time
Range("A1").Select
End Sub
Code:
Sub UpdateLeaveHours2()
Dim ClientForLeave As Single
Dim LeaveHours As Single
Dim ClientNumber As Single
Dim FirstAddress As Single
Sheets("TimeSheetEntry").Select
Do 'should be able to us a better method than a DO loop for this proc?
With ActiveCell 'AC is B2 on TimeSheetEntry sheet
If ActiveCell.Value = 186 Or ActiveCell.Value = 189 Then '
LeaveHours = ActiveCell.Offset(0, 1).Value
ClientNumber = ActiveCell.Offset(0, -1).Value 'client # in Col A
'info is in variables LeaveHours and ClientInfo. Go to CLNTINFO sheet now.
Set ClientForLeave = Worksheets("CLNTINFO").Range("A:A").Find(What:=ClientNumber, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not ClientForLeave Is Nothing Then
FirstAddress = ClientForLeave.Address
Do 'in col A
ClientForLeave.Value = ActiveCell.Offset(0, 6).Value - LeaveHours
Set ClientForLeave = .FindNext(ClientForLeave)
Loop While Not ClientForLeave Is Nothing And ClientForLeave.Address <> FirstAddress
End If
End If
End With
Loop Until ActiveCell > 189 'On TimeSheetEntry sheet. anything higher is not leave time
End Sub