运行每个范围单元循环时避免错误?

时间:2014-11-07 14:18:34

标签: vba excel-vba excel

我写了下面这个函数。通常它工作正常。但是当r是Nothing时,我在行上收到一条错误消息c in r.Cells告诉我“对象变量或者没有设置块变量”。

我不知道为什么会发生这种情况。我想象如果r是Nothing,那么循环就不会运行。

Function CSVFromRange(r As Range) As String
    Dim sTemp As String
    Dim c As Range

    'Append value and comma
    For Each c In r.Cells
        sTemp = sTemp & "," & c.value
    Next c

    'Remove first comma
    If Len(sTemp) > 0 Then
        sTemp = Right(sTemp, Len(sTemp) - 1)
    End If

    CSVFromRange = sTemp
End Function

请告诉我一个优雅的方法,让这个功能不会抛出错误,如果可以的话,还要告诉我它为什么会抛出错误。

4 个答案:

答案 0 :(得分:7)

为什么会出错?

因为它希望传递有效范围。见这个例子

Sub Sample()
    Dim r As Range
    Dim ret

    ret = CSVFromRange(r)

End Sub

Function CSVFromRange(r As Range) As String
    CSVFromRange = r.Value
End Function

这是在您的情况下创建错误的最短方法。传递范围对象但未初始化,因此您将收到错误。您需要确保传递有效范围。例如

Sub Sample()
    Dim r As Range
    Dim ret

    Set r = ThisWorkbook.Sheets("Sheet1").Range("A1")
    ret = CSVFromRange(r)

End Sub

我们如何处理此错误?

您可以在调用sub或函数本身中处理它。我会告诉你们两个

在Sub

Sub Sample()
    Dim r As Range
    Dim ret

    If Not r Is Nothing Then ret = CSVFromRange(r)

End Sub

Function CSVFromRange(r As Range) As String
    CSVFromRange = r.Value
End Function

在功能

Sub Sample()
    Dim r As Range
    Dim ret

    ret = CSVFromRange(r)
End Sub

Function CSVFromRange(r As Range) As String
    If Not r Is Nothing Then
        CSVFromRange = r.Value
    End If
End Function

Sub Sample()
    Dim r As Range
    Dim ret

    ret = CSVFromRange(r)
End Sub

Function CSVFromRange(r As Range) As String
    On Error GoTo ExitGracefully

    CSVFromRange = r.Value

    Exit Function
ExitGracefully:
    MsgBox Err.Description '<~~ comment this if you do not want any alerts
End Function

答案 1 :(得分:2)

最简单的答案是测试rNothing

Function CSVFromRange(r As Range) As String
    Dim sTemp As String
    Dim c As Range

    If r = Nothing Then
        sTemp = vbNullString  'You can change this to a different default return value
    Else
        'Append value and comma
        For Each c In r.Cells
            sTemp = sTemp & "," & c.value
        Next c

        'Remove first comma
        If Len(sTemp) > 0 Then
            sTemp = Right(sTemp, Len(sTemp) - 1)
        End If
    Endif

    CSVFromRange = sTemp
End Function

答案 2 :(得分:1)

我使用On Error Goto你可以看到代码:

Function CSVFromRange(r As Range) As String
    Dim sTemp As String
    Dim c As Range

    On Error GoTo DisplayError
    'Append value and comma
    For Each c In r.Cells
        sTemp = sTemp & "," & c.value
    Next c

    'Remove first comma
    If Len(sTemp) > 0 Then
        sTemp = Right(sTemp, Len(sTemp) - 1)
    End If

    CSVFromRange = sTemp
    Exit Function

    DisplayError:
   ... Display a messagebox with error
End Function

答案 3 :(得分:1)

以上答案已回答您提出的具体问题。我还建议删除底部的if / end if,而不是For循环中的所有内容。当存在空单元格时,isempty条件将阻止双逗号。

Function CSVFromRange(r As Range) As String
    Dim sTemp As String
    Dim c As Range
    'Append value and comma
    For Each c In r.Cells
        If Not IsEmpty(c) And c <> r.Cells.Item(1) Then
            sTemp = sTemp & "," & c
        Else
            sTemp = sTemp & c
        End If
    Next c
    CSVFromRange = sTemp
End Function
相关问题