Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 比较日期以填充适当的颜色_Vba_Excel_If Statement - Fatal编程技术网

Vba 比较日期以填充适当的颜色

Vba 比较日期以填充适当的颜色,vba,excel,if-statement,Vba,Excel,If Statement,使用excel中的VBA,我有一个代码,可以将输入的日期与当前日期进行比较,并根据结果,系统将以正确的颜色填充单元格 其中代码在四种情况下进行比较。 如果输入的日期减去当前日期为: =0 小于0 在1到4之间 在4到10之间 使用IF语句,但系统给我一个错误,错误在哪里,如何修复 代码: Private子命令按钮1\u单击() 作为整数的Dim i 对于i=范围(“C5000”)。结束(xlUp)。行至第2步-1'范围高达5000,请根据您的要求进行更改' 如果是空的(单元格(i,3)),则

使用excel中的VBA,我有一个代码,可以将输入的日期与当前日期进行比较,并根据结果,系统将以正确的颜色填充单元格

其中代码在四种情况下进行比较。 如果输入的日期减去当前日期为:

  • =0
  • 小于0
  • 在1到4之间
  • 在4到10之间
使用IF语句,但系统给我一个错误,错误在哪里,如何修复

代码:
Private子命令按钮1\u单击()
作为整数的Dim i
对于i=范围(“C5000”)。结束(xlUp)。行至第2步-1'范围高达5000,请根据您的要求进行更改'
如果是空的(单元格(i,3)),则退出Sub
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())<0然后
单元格(i,3).Interior.Color=vbGreen
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())=0
单元格(i,3).Interior.Color=vbYellow
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())>1和(VBA.CDate(Cells(i,3))-VBA.Date())<4
单元格(i,3).Interior.Color=vbBlue
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())>4和(VBA.CDate(Cells(i,3))-VBA.Date())<10
单元格(i,3).Interior.Color=vbRed
如果结束
下一个
端接头

退出子项放入下一行。或者更好的是,删除这行代码以允许处理所有行,如

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


       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
       Debug.Print VBA.CDate(Cells(i, 3)) - VBA.Date()
              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
       Debug.Print VBA.CDate(Cells(i, 3)) - VBA.Date()
               Cells(i, 3).Interior.Color = vbRed

       Else
       Cells(i, 3).Interior.Color = vbWhite
       End If

Next
End Sub
Private子命令按钮1\u单击()
作为整数的Dim i
对于i=范围(“C5000”)。结束(xlUp)。行至第2步-1'范围高达5000,请根据您的要求进行更改'
如果为空(单元格(i,3)),则
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())<0然后
单元格(i,3).Interior.Color=vbGreen
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())=0
单元格(i,3).Interior.Color=vbYellow
ElseIf(VBA.CDate(Cells(i,3))-VBA.Date())>=1和(VBA.CDate(Cells(i,3))-VBA.Date())4和(VBA.CDate(Cells(i,3))-VBA.Date())<10
Debug.Print VBA.CDate(单元格(i,3))-VBA.Date()
单元格(i,3).Interior.Color=vbRed
其他的
单元格(i,3).Interior.Color=vbWhite
如果结束
下一个
端接头

为什么要使用VBA?条件格式可以做这类事情。在任何情况下,如果您有错误,请更具体地说。抛出错误的是哪一行?什么错误消息?我尝试使用条件格式化,但它没有工作,因为我想你解决了我的错误谢谢。但现在我有一个问题,如果结果在1到4之间或更多,颜色将是蓝色。我调整了我的答案,现在你应该得到所有的颜色正确。如果日期差超过10天,单元格颜色将变为白色。。。
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


       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
       Debug.Print VBA.CDate(Cells(i, 3)) - VBA.Date()
              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
       Debug.Print VBA.CDate(Cells(i, 3)) - VBA.Date()
               Cells(i, 3).Interior.Color = vbRed

       Else
       Cells(i, 3).Interior.Color = vbWhite
       End If

Next
End Sub