当范围包含非整数值时,使用“Int”函数计算范围

时间:2018-02-15 17:50:18

标签: excel excel-vba loops if-statement vba

我希望我的循环能够评估范围内的值,并在值为非零和整数时继续。我的范围还包含字母数字值,我希望忽略这些值。

当我点击其中一个字母数字值时,我的循环卡住了。我该如何解决?

我的代码:

Dim r2 As Range
Dim fnd As Range
Dim Answer3 As String
    For Each r2 In WorkRng2
        If r2.Value <> "" And WorksheetFunction.IsNumber(r2.Value) Then
            If r2.Value = Int(r2.Value) Then
            Set fnd = WorkRng1.Find(what:=r2.Value, LookIn:=xlValues)
                If fnd Is Nothing Then
                'nothing found
                Answer3 = MsgBox("Cannot proceed! Major Task " & r2.Value & " could not be found in invoice review file!", vbOKOnly, "Question!")
                    If Answer3 = vbOK Then
                        Exit Sub
                    Else
                    'fnd holds the cell that was found
                    End If
                End If
            End If
        End If
    Next

此外,WorkRng1WorkRng2是公共变量

1 个答案:

答案 0 :(得分:1)

对于那些想知道我如何在下面的评论的帮助下解决的人是我的工作代码:

Application.ScreenUpdating = False
    WorkRng1.NumberFormat = "@"
    WorkRng2.Parent.Activate
    WorkRng2.NumberFormat = "@"
    WorkRng1.Parent.Activate
Application.ScreenUpdating = True
Dim r2 As Range
Dim fnd As Range
Dim Answer3 As String
    For Each r2 In WorkRng2
        If r2.Value <> "" And WorksheetFunction.IsNumber(r2.Value) Then
            If r2.Value = Int(r2.Value) And r2.Value <> "9" Then
            Set fnd = WorkRng1.Find(what:=r2.Value, LookIn:=xlValues, LookAt:=xlWhole)
                If fnd Is Nothing Then
                'nothing found
                Answer3 = MsgBox("Cannot proceed! Major Task '" & r2.Value & "' could not be found in invoice review file!", vbOKOnly, "Question!")
                    If Answer3 = vbOK Then
                        Exit Sub
                    Else
                    'fnd holds the cell that was found
                    End If
                End If
            End If
        End If
    Next
Application.ScreenUpdating = False
    WorkRng1.NumberFormat = "0.0"
    WorkRng2.Parent.Activate
    WorkRng2.NumberFormat = "0.0"
    WorkRng1.Parent.Activate
Application.ScreenUpdating = True