Sub DataFormat()
Dim i As Long, LastRow As Long
LastRow = Worksheets("QC").Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
If Worksheets("QC").Cells(i, "K").Value = "MM/01/YYYY" Then
Worksheets("QC").Cells(i, "K").Interior.ColorIndex = 2
Else
Worksheets("QC").Cells(i, "K").Interior.ColorIndex = 3
End If
Next i
End Sub
我只是验证单元格 K 中的值。它的日期为 11/1/2017,单元格的格式为 11 月 17 日。通过上面的代码,我正在验证它是否包含当月的第一天并且此代码不起作用。
您的问题主要来自
Worksheets("QC").Cells(i, "K").value
这不是检查正确的日期。要检查日期格式,您需要添加.NumberFormat
此外,您使用mm/dd/yyyy
的数字格式与您在 excel 中的 11 月 17 日表格中的数字格式不同,因此您可以将其删除。
现在为了检查一个月中的哪一天,您可以简单地使用Day()
仅适用于内部字符串的函数。
Option Explicit
Sub DataFormat()
Dim i As Long, LastRow As Long
Dim daydate As String
LastRow = Worksheets("QC").Cells(Rows.Count, "A").End(xlUp).Row
For i = 3 To LastRow
daydate = Worksheets("QC").Cells(i, "K").Value
If IsDate(daydate) Then
If Day(daydate) = 1 Then
Worksheets("QC").Cells(i, "K").Interior.ColorIndex = 2
Else
Worksheets("QC").Cells(i, "K").Interior.ColorIndex = 3
End If
Else
Worksheets("QC").Cells(i, "K").Interior.ColorIndex = 3
End If
Next i
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句