Hi !
I've tried taking the following code apart several times and building in stages, however I'm still getting foxed by "Next without For" errors and the Next -For loop not working the way I want !!
I have this working using lots of select cases and gotos, but I wanted to try and do it 'properly'. I think I'm being hampered by my own inexperience !!!
The premise is that if a cell on the Absence Report sheet is missing a value then the macro should note the employee number and the date, then look to the Sick_AltDuty sheet to match the information and find the missing value.
If needed I have a test file that I can upload tonight ...
Any suggestions would be welcome, I'm guessing it's to do with the many IFs, however I can't get the phrasing correct.
I've tried taking the following code apart several times and building in stages, however I'm still getting foxed by "Next without For" errors and the Next -For loop not working the way I want !!
I have this working using lots of select cases and gotos, but I wanted to try and do it 'properly'. I think I'm being hampered by my own inexperience !!!
The premise is that if a cell on the Absence Report sheet is missing a value then the macro should note the employee number and the date, then look to the Sick_AltDuty sheet to match the information and find the missing value.
Code:
For o = 1 To LastRow
Sheets("Absence Report").Select
If Cells(rowcount, 4) = "" Then 'if this cell is empty then
If Cells(rowcount, 3) = "" Then 'if date cell is empty then end of this section
MsgBox ("end")
Exit For
Else
EmpNo = Cells(rowcount, 1) 'if date cell is not empty then declare employee number ...
AbDate = Cells(rowcount, 3) '...and date of absence
rowcount = rowcount + 1 'add one to the row counter
End If
Sheets("Sick_AltDuty").Select
Range("B3").Select
For p = 1 To LastRow2 'we need to find the employee number and date that matches the earlier value
OldEmpNo = Cells(rowcount2, 2)
If OldEmpNo = EmpNo Then 'if the current cell = the emp no ...
OldAbDate = Cells(rowcount2, 1)
If OldAbDate = AbDate Then AbReason = Cells(rowcount2, 9) 'if the current cell = the absence date ...
GoTo AddReason 'goto addreason
If OldAbDate = 0 Then AbReason = "Date Not Found" 'if no date then enter text
GoTo AddReason 'goto addreason
Else
rowcount2 = rowcount2 + 1 'otherwise this date doesn't match, add one to the counter...
End If
Next p '...next p to find the right emp and date
If OldEmpNo = "" Then 'but if the emp can't be found then enter text
AbReason = "Employee Not Found"
GoTo AddReason 'goto addreason
Else
rowcount2 = rowcount2 + 1 'otherwise this employee doesn't match
Next p '... next p to find the right emp and date
End If
Else
If Cells(rowcount, 4) <> "" Then rowcount = rowcount + 1 'if the cell isn't blank then add to row count
End If
Next o
If needed I have a test file that I can upload tonight ...
Any suggestions would be welcome, I'm guessing it's to do with the many IFs, however I can't get the phrasing correct.