确定具有日期的单元格是否包含特定年份

时间:2017-07-11 17:47:27

标签: excel vba excel-vba

我正在尝试使用VBA迭代excel中的列,以确定是否有任何单元格包含某一年。我想继续迭代,看看它是否真实。我正在查看的列包含格式为dd / mm / yyyy的日期。下面是我正在迭代的列的示例,后面是我想要更改的一些代码。

C1: (05/01/2013, 06/07/2015, 09/08/2019, ... )

yy = 2018
for i = 1 to 500
    if cells(i,1) contains "yy" then
        cells(i,2) = "yes"
    end if
next i

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

尝试使用Excel =YEAR()功能。像这样:

C1: (05/01/2013, 06/07/2015, 09/08/2019, ... )

yy = 2018
While i <= 500
    if Year(ActiveWorksheet.Cells(i,1)) = yy Then
        ActiveWorksheet.Cells(i,2).Value = "Yes"
    end If
    i = i + 1
Loop

答案 1 :(得分:0)

试试这个:

Sub FindYear()

yy = 2018
'itarating from first row to last row with value
From i=1 To ActiveWorksheet.Range("A1048576").End(xlUp).Row
If Year(ActiveWorksheet.Range("A" & i).Value = yy Then
    ActiveWorksheet.Range("B" & i).Value = "yes"
End If

End Sub