CInt在VBA中使用(跳过空单元格)

时间:2015-05-12 19:27:19

标签: excel excel-vba vba

CInt中使用VBA,但有一些空单元格。它需要跳过并转到下一个

Do While i < 400
    Str = Cells(i, NOfMonths)

    Num = CInt(Str)
    If Num > 120 And Num < 500 Then
        Cells(i, 13) = ">10"

    Else
        ws2.Select
        On Error Resume Next
        result = Application.VLookup(Str, ws2.Range("A3:B125"), 2, False)

        If (Err <> 0) Then
        Else
            ws1.Select
            Cells(i, 13) = result
        End If
    End If

1 个答案:

答案 0 :(得分:0)

只需检查单元格是否为空。

Do While i < 400
    Str = Cells(i, NOfMonths)

    '~~> Check for blank cell
    If Len(Trim(Str)) <> 0 Then
        Num = CInt(Str)
        If Num > 120 And Num < 500 Then
            Cells(i, 13) = ">10"
        Else
            ws2.Select
            On Error Resume Next
            result = Application.VLookup(Str, ws2.Range("A3:B125"), 2, False)

            If (Err <> 0) Then
            Else
                ws1.Select
                Cells(i, 13) = result
            End If
        End If
    End If
Loop

另外请不要将Reserved个词用作变量。 Str是保留字。您可以使用Strg代替。