对If / Else使用等效的VBA Try / Except

时间:2019-05-13 16:14:45

标签: excel vba error-handling

我试图遍历某些电子表格范围,并使用try / except来构建if / else语句。我这样做的原因是因为IsNumeric()对我不起作用,所以我试图做这样的事情(尝试从python格式化除外)

Dim Temp as Integer
Dim Myrange as Range
Dim Myrow as Range
Set Myrange = Range("A1","A1000")
For Each Myrow in Myrange.Row
  If IsEmpty(Range("A" & Myrow.Row)) Then  
    Exit For 'To escape the loop at the end of the filled cells
  Else
    Try:
      Temp = (Myrow.Value() - 0) 'This causes a #VALUE! error when the Myrow.Value is not a number.
    Except:
        Range("B" & Myrow.Row).Value = Temp 'this sets the value of the rightmost cell to whatever current value of Temp is.  

我还尝试了其他一些错误捕获,但似乎无法在VBA中得到它。

For Each Myrow In Myrange.Rows
       If IsEmpty(Range("A" & Myrow.Row)) Then
         Exit For
       Else
           On Error Resume Next
           Temp = Myrow.Value() - 0
           If Err.Number = 0 Then
               Range("A" & Myrow.Row).Value = ""
           ElseIf Err.Number <> 0 Then
               Range("B" & Myrow.Row) = Temp
           End If
       End If
   Next Myrow

我真的只是在寻找清单的底端内容,请参阅第一个数字,设置B0:Bn1 = Temp的值,当命中An(新数字)时,Temp的值将更改为temp2,然后更改为单元格Bn1 + 1 -> Bn2-1是temp2,直到找到新的数字为止。

在工作表中,我可以通过向下拖动公式=(A1-0)来解决这些错误消息,这些错误消息不是数字型的,但由于某些原因我无法对其进行编码。


使用@MathieuGuindon的建议,通过使用变体类型并对其进行测试,解决了该问题。解决方案代码:

    Dim Myrange As Range
    Dim Myrow As Range
    Dim Temp As Variant
    Dim NextTemp As Variant

    Set Myrange = Selection
    For Each Myrow In Myrange.Rows
        NextTemp = Range("A" & Myrow.Row).Value
        If IsEmpty(Range("A" & Myrow.Row)) Then
          Exit For
        ElseIf IsNumeric(NextTemp) Then
            Temp = NextTemp
            Range("A" & Myrow.Row).Value = ""
        Else
            Range("B" & Myrow.Row).Value = Temp
                End If
    Next Myrow

3 个答案:

答案 0 :(得分:1)

进行一些简化,然后听取Mathieu的评论,尝试一下。不确定您在做什么,但这可能不太正确。

Sub x()

Dim Temp As Variant
Dim Myrange As Range
Dim Myrow As Range

Set Myrange = Range("A1", "A1000")

For Each Myrow In Myrange
    If Not IsEmpty(Myrow) Then
        Temp = Myrow.Value - 0
            If IsNumeric(Temp) Then
               Myrow.Value = vbNullString
            Else
                Myrow.Offset(, 1).Value = Temp
            End If
    End If
Next Myrow

End Sub

答案 1 :(得分:0)

一种方法是在子标题的末尾有一个专用的错误处理程序,并检查错误代码(类型不匹配为13):

Option Explicit

Public Sub EnumerateValues()
    On Error GoTo err_handle

    Dim Temp As Integer
    Dim Myrange As Range
    Dim Myrow As Range
    Dim myNumber As Double ' Int? Long?

    Set Myrange = Range("A1", "A1000")

    For Each Myrow In Myrange.Rows
        If IsEmpty(Range("A" & Myrow.Row)) Then
            Exit For ' to escape loop at end of filled cells
        Else
            myNumber = CDbl(Myrow.Value())
            Debug.Print myNumber
        End If
' use label, since VBA doesn't support Continue in loop.
loop_continue:
    Next Myrow

exit_me:
    Exit Sub

err_handle:
    Select Case Err.Number
        Case 13 ' Type Mismatch
            GoTo loop_continue
        Case Else
            MsgBox Err.Description, vbOKOnly + vbCritical, Err.Number
            GoTo exit_me
    End Select
End Sub

这样,如果遇到CDbl(或等效函数)失败的值,我们将继续进行下一行。

答案 2 :(得分:0)

尽管第一个示例包含Try:Except:作为标签,但它们不提供错误控制。尝试/例外是个错误控制方法,而不是

目前尚不清楚您在A列中是否有看起来像数字的文本。如果Temp = (Myrow.Value() - 0)仅用于确定A列中的值是否是数字并且不用作转换,则SpecialCells可以快速找到A列中的数字。

dim rng as range

on error resume next
'locate typed numbers in column A
set rng = Range("A:A").SpecialCells(xlCellTypeConstants, xlNumbers)
on error goto 0

If not rng is nothing then
    rng = vbNullString
End If

on error resume next
'locate text values in column A
set rng = Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues)
on error goto 0

If not rng is nothing then
    rng.Offset(0, 1) = rng.Value
End If

您还可以使用xlCellTypeFormulas返回数字或公式返回的文本。