VBA不匹配计算天数
在我的excel电子表格中,我的代码工作了大约2个月。现在,当我现在运行代码时,我得到一个不匹配的类型13错误。代码中没有任何更改。我的日期在第J列和第K列,起始单元格是#3,因为前两个是标题。第L列为空白,日期等于8。单元格颜色变为绿色,文本添加“电子邮件”,文本颜色也变为白色。其他单元格以数值形式显示日期,我将它们设置为非活动状态,因为我只使用它们来证明公式。谢谢你的帮助VBA不匹配计算天数,vba,excel,Vba,Excel,在我的excel电子表格中,我的代码工作了大约2个月。现在,当我现在运行代码时,我得到一个不匹配的类型13错误。代码中没有任何更改。我的日期在第J列和第K列,起始单元格是#3,因为前两个是标题。第L列为空白,日期等于8。单元格颜色变为绿色,文本添加“电子邮件”,文本颜色也变为白色。其他单元格以数值形式显示日期,我将它们设置为非活动状态,因为我只使用它们来证明公式。谢谢你的帮助 Dim mydate1 As Date Dim mydate2 As Long Dim datetoday1 As Da
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim Lastrow As String
Dim x As Long
Lastrow = Sheets("TWO_List").Cells(Rows.Count, 1).End(xlUp).Row
For x = 3 To Lastrow 'Starting row
mydate1 = Cells(x, 11).Value ' TWO Required Completion Date
mydate2 = mydate1 'selects value to mydate2
'Cells(x, 41).Value = mydate2 'displays expire date
datetoday1 = Date 'sets date
datetoday2 = datetoday1 'selects todays date in datetoday2
'Cells(x, 42).Value = datetoday2 'display todays date
If mydate2 - datetoday2 = 8 Then 'Number of days email sent before TWO expired
Cells(x, 12) = "Emailed" 'places Yes in Reminder cells
Cells(x, 12).Interior.ColorIndex = 10 'Color format
Cells(x, 12).Font.ColorIndex = 2 'text color
Cells(x, 12).Font.Bold = True 'Changes to bold text
将最后一行的长度设置为
而不是字符串
,并且日期应声明为日期
类型
类似这样的工作原理:
Option Explicit
Sub TestMe()
Dim mydate1 As Date
Dim mydate2 As Date
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim Lastrow As String
Dim x As Long
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 3 To Lastrow
mydate1 = Cells(x, 1).Value
mydate2 = mydate1
datetoday1 = Date
datetoday2 = datetoday1
If DateDiff("d", mydate2, datetoday2) = 8 Then
Cells(x, 2) = "Emailed"
Cells(x, 2).Interior.ColorIndex = 10
Cells(x, 2).Font.ColorIndex = 2
Cells(x, 2).Font.Bold = True
End If
Next x
End Sub
最有可能的情况是,如果它一直在工作,则错误在数据中,可能您的某个“日期”实际上是看起来像日期而不是真实日期的文本。如果您遇到不匹配,则您的某个日期不是真实日期@John@John在错误之前写入
MsgBox mydate2 datetoday2
,并查看MsgBox
。正如@Scott Craner所说,最有可能的一个日期是无效的(例如2018年2月29日)。Ok尝试了上述公式,但它不识别k行中的过期日期。@John-这些日期看起来如何?你能做一个屏幕截图并提出问题吗?我很抱歉耽搁了,我接到一个项目的通知还不到24小时。我发现了问题,一切正常。谢谢大家的帮助。非常感谢!