excel:vbatypes不匹配比较date
我试图比较两个date(不同列)和今年的date。 如果年份相同,则应在第13栏的相应行上写上“ATUAL”一词,如果年份不同,则不应写任何内容。
这是我迄今为止所尝试的。
Sub CopyColumn2() Dim i As Long Dim j As Long Dim lastrow As Long Dim ws1 As Worksheet Dim ws2 As Worksheet Dim wbk As Workbook Dim wb As Worksheet Set wbk = Workbooks.Open("I:\CGP\DEOPEX\01 - Supervisão\01 - Administrativo\06- ADM - Taís e Natalia\Férias - Aprovadas\FÉRIAS TÉCNICOS EXTERNAS.xlsx") Set ws1 = ThisWorkbook.Sheets("BASE_TOTAL") Set ws2 = wbk.Worksheets("FUNCIONÁRIOS") lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row For j = 2 To lastrow If Year(ws1.Cells(j, 9)) = Year(Date) Or Year(ws1.Cells(j, 12)) = Year(Date) Then ws1.Cells(j, 13) = "ATUAL" Else ws1.Cells(j, 13) = "" End If Next j End Sub
date放置在列I和L中,所有列都设置为date。 我的Excel是用葡萄牙语,所以我的date格式是dd / mm / yyyy。
当我运行我的代码时,我收到以下消息:
运行时错误13:types不匹配
这部分是强调:
If Year(ws1.Cells(j, 9)) = Year(Date) Or Year(ws1.Cells(j, 12)) = Year(Date) Then
有人知道这里有什么问题吗? 它应该工作,因为我所有的date格式都是一样的。
尝试这个:
- 声明datevariables
Dim date1 As Date, date2 As Date
-
为variables设置值
date1 = ws1.Cells(j, 9) date2 = ws1.Cells(j, 12)
-
在你的
If
语句中使用variablesIf Year(date1) = Year(Date) Or Year(date2) = Year(Date) Then