根据使用VBA假期名单表中输入假期
|问题:
我在工作表中有EmployeeName,HolidayStart和HolidayEnd。如何在下个月的工作表中为每个员工的假期上色?
答案:
在ModuleSheet中以XL5 / 7输入以下代码,在常规模块中以XL8输入以下代码,将其分配给Button并运行它。
将下面的代码放入标准模块
Sub NewVacation() Dim rngFind As Range Dim intRow As Integer, intMonth As Integer, intCounter As Integer intRow = 3 Do Until IsEmpty(Cells(intRow, 1)) For intMonth = Month(Cells(intRow, 2)) To Month(Cells(intRow, 3)) Set rngFind = Worksheets(Format(DateSerial(1, intMonth, 1), "mmmm")). _ Columns(1).Find _ (Cells(intRow, 1), LookIn:=xlValues, lookat:=xlWhole) If intMonth = Month(Cells(intRow, 2)) And intMonth = _ Month(Cells(intRow, 3)) Then For intCounter = Day(Cells(intRow, 2)) To Day(Cells(intRow, 3)) rngFind.Offset(0, intCounter).Interior.ColorIndex = 3 Next intCounter ElseIf intMonth = Month(Cells(intRow, 2)) Then For intCounter = Day(Cells(intRow, 2)) To Day(DateSerial _ (1, Month(Cells(intRow, 2)) + 1, 0)) rngFind.Offset(0, intCounter).Interior.ColorIndex = 3 Next intCounter Else For intCounter = 1 To Day(Cells(intRow, 3)) rngFind.Offset(0, intCounter).Interior.ColorIndex = 3 Next intCounter End If Next intMonth intRow = intRow + 1 Loop End Sub