mardi 10 octobre 2017

vba excel compaing between dates in order to fill the approperiate color

using the VBA in excel i have a code that compare the entered dates with the current date and based on the result the system will fill the cell in the right color.

where the code compare in four conditions. if the entered date minus the current is :

  • = 0
  • less then 0
  • between 1 and 4
  • between 4 and 10

using the IF statement but the system give me an error where is the error and how to fix it ?

code:

Private Sub CommandButton1_Click()

Dim i As Integer


For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'


       If IsEmpty(Cells(i, 3)) Then Exit Sub

       ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
              Cells(i, 3).Interior.Color = vbGreen

       ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
             Cells(i, 3).Interior.Color = vbYellow

       ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) > 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 4 Then
              Cells(i, 3).Interior.Color = vbBlue

       ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) > 4 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 10 Then
               Cells(i, 3).Interior.Color = vbRed


End If

Next
End Sub

Aucun commentaire:

Enregistrer un commentaire