Excel 使用On Error Goto尝试查找我出错的原因
当我运行代码时,类型不匹配。有没有办法使用On Error Goto来帮助我调试它?这是我的密码Excel 使用On Error Goto尝试查找我出错的原因,excel,vba,error-handling,Excel,Vba,Error Handling,当我运行代码时,类型不匹配。有没有办法使用On Error Goto来帮助我调试它?这是我的密码 Private Sub FH_CNC_HideOrders_Click() On Error GoTo errHandler If Me.FH_CNC_HideOrders.Caption = "Hide" Then 'Intiansiate objects and setup variables Dim tbl As ListObject
Private Sub FH_CNC_HideOrders_Click()
On Error GoTo errHandler
If Me.FH_CNC_HideOrders.Caption = "Hide" Then
'Intiansiate objects and setup variables
Dim tbl As ListObject
Dim c As Range
Dim colStartDate As Range
Dim FoundDate As Date
'Set object/variable values
Set tbl = ActiveWorkbook.Worksheets("Production Tracking").ListObjects("Table293")
With tbl
'Search "Start Date" (Col2), top to bottom, searching for the first cell with a color index of 15 and the "End Date" (Col3) which has an index color of anything other than 15
Set colStartDate = .ListColumns("CNC Begins").DataBodyRange
For Each c In colStartDate.Cells
'MsgBox "c.Value:" & c.Value & " | c.Interior.ColorIndex:" & c.Interior.ColorIndex & " | c.Address:" & c.Address _
& Chr(10) & Chr(10) & "c.Offset.Value):" & c.Offset(0, 1).Value & " | c.Interior.ColorIndex:" & c.Offset(0, 1).Interior.ColorIndex & " | c.Address:" & c.Offset(0, 1).Address
If c.Interior.ColorIndex = 15 And c.Offset(0, 1).Interior.ColorIndex <> 15 Then
FoundDate = c.Value
Exit For
End If
Next c
For Each c In colStartDate.Cells
If Not c.EntireRow.Hidden = True Then
errHandler:
Msbox c.Value
Exit Sub
'Hide dates prior to colStartDate but not empty cells
If Not IsEmpty(c.Value) Then
If Not c.Value >= FoundDate And IsDate(c.Value) Then
c.EntireRow.Hidden = True
'MsgBox c.Address
End If
End If
End If
Next c
End With
Me.FH_CNC_HideOrders.Caption = "Show"
ElseIf Me.FH_CNC_HideOrders.Caption = "Show" Then
Me.FH_CNC_HideOrders.Caption = "Hide"
End If
End Sub
Private Sub FH\u CNC\u HideOrders\u Click()
关于错误转到错误处理程序
如果Me.FH\u CNC\u HideOrders.Caption=“Hide”则
'初始化对象和设置变量
作为ListObject的Dim tbl
调光范围
暗淡的冷启动日期范围
将日期设置为日期
'设置对象/变量值
设置tbl=ActiveWorkbook.Worksheets(“生产跟踪”).ListObjects(“Table293”)
带tbl
'搜索“开始日期”(Col2),从上到下,搜索颜色索引为15的第一个单元格和索引颜色不是15的“结束日期”(Col3)
设置colStartDate=.ListColumns(“CNC开始”).DataBodyRange
对于colStartDate.单元格中的每个c
“MsgBox”c.Value:&c.Value&&c.Interior.ColorIndex:&c.Interior.ColorIndex&&c.Address:&c.Address_
&Chr(10)和Chr(10)以及“c.Offset.Value:”&c.Offset(0,1)。Value和“&c.Interior.ColorIndex:”&c.Offset(0,1)。Interior.ColorIndex和“&c.Offset(0,1)。Address:”&c.Offset(0,1)。Address
如果c.Interior.ColorIndex=15和c.Offset(0,1).Interior.ColorIndex为15,则
FoundDate=c.值
退出
如果结束
下一个c
对于colStartDate.单元格中的每个c
如果不是c.EntireRow.Hidden=True,则
错误处理程序:
Msbox c.值
出口接头
'隐藏colStartDate之前的日期,但不隐藏空单元格
如果不是空的(c值),则
如果不是c.值>=FoundDate和IsDate(c.值),则
c、 EntireRow.Hidden=True
'MsgBox c.地址
如果结束
如果结束
如果结束
下一个c
以
Me.FH\u CNC\u HideOrders.Caption=“Show”
ElseIf Me.FH\u CNC\u HideOrders.Caption=“Show”然后
Me.FH\u CNC\u HideOrders.Caption=“隐藏”
如果结束
端接头
我在代码中放置了一条注释,如果发生错误,我希望MsgBox
该值 你“可以”,但我真的不明白你为什么“应该”
如果由于c
包含非日期而可能出现不匹配错误,为什么不测试c
并找出原因
If IsDate(c.Value) Then ...
或者不是测试它是否是日期,而是找出它是什么类型的日期
Select Case VarType(c.Value)
Case 2 to 6
MsgBox "These are not dates"
Exit Sub
Case 7
c.EntireRow.Hidden = True
Case Else
....
或者如果你不想为那些VBA常量而烦恼
If TypeName(c.Value) = "String" Then MsgBox "This is not a date"
你“可以”,但我真的不明白你为什么“应该”
如果由于c
包含非日期而可能出现不匹配错误,为什么不测试c
并找出原因
If IsDate(c.Value) Then ...
或者不是测试它是否是日期,而是找出它是什么类型的日期
Select Case VarType(c.Value)
Case 2 to 6
MsgBox "These are not dates"
Exit Sub
Case 7
c.EntireRow.Hidden = True
Case Else
....
或者如果你不想为那些VBA常量而烦恼
If TypeName(c.Value) = "String" Then MsgBox "This is not a date"
让我们明确这一点。错误处理不应用于查找代码中的问题。VBA在这方面已经做得很好,它将停止程序或拒绝编译 错误处理适用于异常情况,即当您使用您无法控制的资源时,这可能会导致您的程序由于自身的错误而崩溃。在这种情况下,您应该捕获错误并决定如何处理它 要消除代码中的逻辑错误,请确保已执行以下操作
祝你好运让我们弄清楚这一点。错误处理不应用于查找代码中的问题。VBA在这方面已经做得很好,它将停止程序或拒绝编译 错误处理适用于异常情况,即当您使用您无法控制的资源时,这可能会导致您的程序由于自身的错误而崩溃。在这种情况下,您应该捕获错误并决定如何处理它 要消除代码中的逻辑错误,请确保已执行以下操作