Deleting staff names when they are on annual leave

Bill Stenson

New Member
Joined
Jul 24, 2016
Messages
1
Hi,

This is my first post on here, I hope you can help :)

I am building a staffing plan for work. I have the weekly plan setup on sheet 1, which looks like this:

DUTYMTWThFS
1L. FawcettL. FawcettL. FawcettL. FawcettL. FawcettL. Fawcett
2S. MeadS. MeadS. MeadS. MeadS. MeadS. Mead
3S. WoodS. WoodS. WoodS. WoodS. WoodA. Blundell
4P. LeeP. LeeP. LeeP. LeeP. LeeP. Lee

<tbody>
</tbody>

<tbody>
</tbody>

The annual leave for the week is on sheet 2 in the same format. I have been trying (and failing) to build a macro that will read the annual leave list for each day of the week, and delete anyone that is on leave on a given day.

The names in the weekly plan on sheet 1 are selected from a data validation so I can't use any formulas in the cells.

Any advice will be greatly appreciated

Bill
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Based on your description, I named sheet1 "Staff Plan" & sheet2 "Annual Leave". I also am assuming that sheet2 is setup like sheet1, the only difference being is missing names for people who are on leave. If this is the case, see if this works for what you want to do. You may have to change the sheet names and cell references in the code to fit your needs.

Sheet1 "Staff Plan" before macro:

Excel 2012
ABCDEFGH
1DUTYMTWThFS
21L. FawcettL. FawcettL. FawcettL. FawcettL. FawcettL. Fawcett
32S. MeadS. MeadS. MeadS. MeadS. MeadS. Mead
43S. WoodS. WoodS. WoodS. WoodS. WoodA. Blundell
54P. LeeP. LeeP. LeeP. LeeP. LeeP. Lee
6
Staff Plan


Sheet2 "Annual Leave":

Excel 2012
ABCDEFGH
1MTWThFS
2L. FawcettL. FawcettL. FawcettL. Fawcett
3S. MeadS. Mead
4S. WoodS. WoodS. Wood
5P. LeeP. LeeP. Lee
6
7
Annual Leave


Code:
Sub Staff_Plan_Annual_Leave()
Dim i As Long, j As Long, lastcol As Long, lastrow As Long

lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
lastrow = Cells(Rows.Count, 2).End(xlUp).Row

For i = 2 To lastcol
    For j = 2 To lastrow
        If Sheets("Annual Leave").Cells(j, i).Value = Sheets("Staff Plan").Cells(j, i).Value Then
            Else
        If IsEmpty(Sheets("Annual Leave").Cells(j, i).Value) = True Then
            Sheets("Staff Plan").Cells(j, i).ClearContents
        End If
        End If
    Next j
Next i
        
End Sub

Sheet1 "Staff Plan" after macro runs:

Excel 2012
ABCDEFGH
1DUTYMTWThFS
21L. FawcettL. FawcettL. FawcettL. Fawcett
32S. MeadS. Mead
43S. WoodS. WoodS. Wood
54P. LeeP. LeeP. Lee
6
Staff Plan
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top