我一直在尝试找出如何根据年份在同一工作表的两个不同列中比较日期。 “ S”和“ U”列的日期格式为-mm / dd / yyyy。问题似乎可能仅在于将日期分配给变量“ date1”和“ date2”,但是代码中可能存在更多问题。
Function Condition()
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim wbk As Workbook
Dim wb As Worksheet
Dim date1 As Date, date2 As Date
Set wbk = Application.Workbooks("RELIEF VALVE MASTER SPREADSHEET.xlsx")
Set ws1 = wbk.Worksheets("Valve List")
lastrow = ws1.range("S" & Rows.Count).End(xlUp).Row
For j = 2 To lastrow
date1 = ws1.Cells(j, 18).Value
date2 = ws1.Cells(j, 20).Value
If Year(date1) = Year(Date) - 1 Or Year(date2) <> Year(Date) - 1 Then
Application.Workbooks("MaxiTrak RV Service Report - Blank.xlsm").Activate
ActiveWorkbook.Sheets("ML_PSV_SERVICE").Select
Cells(j, 7).Value = "Routine"
Else
ws1.Cells(j, 7) = ""
End If
Next j
End Function
此代码背后的基本思想是检查同一行Ex:(S2和U2)中的单元格是否具有相同的年份。如果它们是同一年(即,2018年),则将文本“常规”插入同一行中第7列的另一个打开的工作簿中。我在这里先向您的帮助表示感谢。
错误-> date1 = ws1.Cells(j, 18).Value
上的类型不匹配