Excel 我经常得到';运行时错误13';与';mydate=Sheets(“Summary”).单元格(i,“A”).值';
根据以下逻辑,代码隐藏了一些行,但是它在第80行停止并显示此错误。我检查了这些日期和时间的格式,看起来不错。 有人能帮我找出哪里出了问题吗Excel 我经常得到';运行时错误13';与';mydate=Sheets(“Summary”).单元格(i,“A”).值';,excel,error-handling,runtime,vba,Excel,Error Handling,Runtime,Vba,根据以下逻辑,代码隐藏了一些行,但是它在第80行停止并显示此错误。我检查了这些日期和时间的格式,看起来不错。 有人能帮我找出哪里出了问题吗 Public Sub ShowShift3() Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long Dim mydate As Date Dim mytime As Date Dim mystatus As String lastrow1 = Sheets("Summary").Rang
Public Sub ShowShift3()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim mydate As Date
Dim mytime As Date
Dim mystatus As String
lastrow1 = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Summary").Activate
For i = lastrow1 To i = 2 Step -1
mydate = Sheets("Summary").Cells(i, "A").Value
mytime = Sheets("Summary").Cells(i, "B").Value
If (mydate < Date) And (mytime < TimeValue("22:00:00")) Then
Worksheets("Summary").Rows(i).Hidden = True
End If
Next
End Sub
公共子ShowShift3()
尺寸i为长,j为长,lastrow1为长,lastrow2为长
将mydate设置为日期
将我的时间设置为日期
模糊的mystatus作为字符串
lastrow1=工作表(“摘要”).范围(“A”和行数.Count).结束(xlUp).行
工作表(“摘要”)。激活
对于i=lastrow1到i=2步骤-1
mydate=表格(“摘要”).单元格(i,“A”).值
mytime=表格(“摘要”).单元格(i,“B”).值
如果(mydate<日期)和(mytime<时间值(“22:00:00”)),则
工作表(“摘要”).Rows(i).Hidden=True
如果结束
下一个
端接头
错误可能在您的数据中,而不是代码中:
Sub DateCheck()
Dim mydate As Date
i = 1
mydate = Sheets("Summary").Cells(i, "A").Value
End Sub
VBA很可能无法将相应单元格中的值解析为日期 检查以下各项:
Sub TestMe()
Dim a As Date
a = "what"
a = ""
End Sub
”
和“what”
都不能转换为日期。我猜在你的例子中,单元格(I,“A”)
是空的,因此你得到了这个错误
其他变量类型Long
、String
、Variant
、Object
等可以轻松地分配给空单元格的值,它们中的每一个都会相应地解析它:
变为Long
0
变为字符串
”
变为Boolean
False
Public Sub ShowShift3()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim mydate As Date
Dim mytime As Date
Dim mystatus As String
lastrow1 = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
For i = lastrow1 To 2 Step -1
If IsDate(Sheets("Summary").Cells(i, "A").Value) = True Then 'check if the contents in the cell are in fact a date
mydate = Sheets("Summary").Cells(i, "A").Value
mytime = Format(Sheets("Summary").Cells(i, "B").Value, "hh:mm") 'format the time properly for comparison
If (mydate < Date) And (mytime < TimeValue("22:00:00")) Then
Worksheets("Summary").Rows(i).Hidden = True
End If
End If
Next i
End Sub
公共子ShowShift3()
尺寸i为长,j为长,lastrow1为长,lastrow2为长
将mydate设置为日期
将我的时间设置为日期
模糊的mystatus作为字符串
lastrow1=工作表(“摘要”).范围(“A”和行数.Count).结束(xlUp).行
对于i=最后一行1到2步骤-1
如果IsDate(Sheets(“Summary”).Cells(i,“A”).Value)=True,则“检查单元格中的内容是否为日期”
mydate=表格(“摘要”).单元格(i,“A”).值
mytime=格式(表格(“摘要”)。单元格(i,“B”)。值“hh:mm”)'正确设置时间格式以进行比较
如果(mydate<日期)和(mytime<时间值(“22:00:00”)),则
工作表(“摘要”).Rows(i).Hidden=True
如果结束
如果结束
接下来我
端接头
这是有道理的,虽然我检查了单元格是否有正确的内容,但我会再次检查,以确定是否遗漏了任何内容。是的,我在以前的代码中遇到了这件事,检查了它,似乎不是这个错误。@RahulBaranwal也是一个指针,您不需要激活工作表来处理它,你可能已经注意到我从代码中删除了那一行。哦,是的!我注意到了。我总是搞不清楚我们什么时候激活它。是否存在需要激活工作表的一般情况?@RahulBaranwal我还没有遇到必须激活工作表的情况,总是有解决办法(可能是错误的),但激活和选择总是会使代码变慢。同样,如果这有帮助,请将其标记为答案?