如何在VBScript for Loop中检查错误

时间:2018-01-05 18:17:57

标签: excel vbscript

我如何留在" For Loop"如果我有错误。我想用" Unknown"填写Excel表格单元格。如果发生错误。

Function AddCommonDetail(ByVal ColCount)
  for i = 1 to ColCount 
    WorkSheet.Cells(row, i).Value  = DataSet(i).value
  Next

结束功能

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

Function AddCommonDetail(ByVal ColCount)
  ' This tells it to just continue to the next statement if there's an error
  On Error Resume Next

  for i = 1 to ColCount 
    WorkSheet.Cells(row, i).Value = DataSet(i).value
    If Err.Number <> 0 Then
      Err.Clear
      WorkSheet.Cells(row, i).Value = "Unknown"
    End If
  Next