I am encountering a bug in VBA. As I am just a few weeks in, the code itself probably lacks a lot of best practices.
But besides that, in this specific case I get an Overflow
error on the following line
dateDifference = DateDiff("d", currentDate, olderDate, vbMonday)
in the section
'========================
'make cell BLUE
'========================
The full code is listed below. Does anybody have an idea, what is causing this issue? As a greenhorn my guess is, this has to do with constantly reassigning 3 variables in the loop?
Thanks a lot in advance.
Sub HrReporting_Step07_ApplyCellColouring()
ThisWorkbook.Activate
'========================
'Variables for looping
'========================
'declarations
Dim rowCount As Integer
Dim i As Integer
Dim srcColourColumnIntRed1 As Integer
Dim srcColourColumnIntRed2 As Integer
Dim srcColourColumnIntYellow As Integer
Dim srcColourColumnIntGreen As Integer
Dim srcColourColumnIntBlue1 As Integer
Dim srcColourColumnIntBlue2 As Integer
'variable declaration specifically for date calculations that are needed for colouring cells YELLOW or BLUE
Dim olderDate As Date
Dim currentDate As Date
Dim dateDifference As Integer
'assignments
srcColourColumnIntRed1 = Range("Table1[Availability Status]").Column
srcColourColumnIntRed2 = Range("Table1[Sum of Current Calendar % Allocated]").Column
srcColourColumnIntYellow = Range("Table1[Coming Available Category]").Column
srcColourColumnIntGreen = Range("Table1[CW-1]").Column
srcColourColumnIntBlue1 = Range("Table1[Current Calendar]").Column
srcColourColumnIntBlue2 = Range("Table1[Current Calendar End Date]").Column
rowCount = Range("Table1[Coming Available Category]").Count + 1
'========================
'make cell RED
'========================
For i = 2 To rowCount
'based on following conditions
' 1. Column "Sum of Current Calendar % Allocated" is lower or equal to 60 %
' 2. Column "Availability Status" = Now Available
If Cells(i, srcColourColumnIntRed1).Value = "Now Available" _
Or Cells(i, srcColourColumnIntRed2).Value <= 60 _
Then Cells(i, 1).Interior.Color = RGB(255, 0, 0)
Next i
'========================
'make cell YELLOW
'========================
For i = 2 To rowCount
'based on following condition
' 1. Column "Coming Available Category" = Available in the next 2 weeks
If Cells(i, srcColourColumnIntYellow).Value = "Resource First Available Day 1-7 Days" _
Or Cells(i, srcColourColumnIntYellow).Value = "Resource First Available Day 8-14 Days" _
Then Cells(i, 1).Interior.Color = RGB(255, 255, 0)
Next i
'========================
'make cell BLUE
'========================
For i = 2 To rowCount
'based on following conditions
' 1. Column "Current Calendar" unequal to "Booked To A Project"
' 2. Column "Current Calendar" unequal to empty
' 3. Column "Current Calendar End Date" < to 42 days AND > 12 days
olderDate = Cells(i, Range("Table1[Current Calendar End Date]").Column)
currentDate = Date
dateDifference = DateDiff("d", currentDate, olderDate, vbMonday)
If (Cells(i, srcColourColumnIntBlue1).Value <> "Booked To A Project" _
And Cells(i, srcColourColumnIntBlue1).Value <> "") _
Or (dateDifference <= 42 And dateDifference > 14) _
Then Cells(i, 1).Interior.Color = RGB(0, 0, 255)
Next i
'========================
'make cell GREEN
'========================
For i = 2 To rowCount
'based on following condition
' 1. Name does not exist in previous weeks' sheet, identified by VLOOKUP being #N/A
If WorksheetFunction.IsNA(Cells(i, srcColourColumnIntGreen)) _
Then Cells(i, 1).Interior.Color = RGB(0, 255, 0)
Next i
End Sub