VBA识别范围内的数字

时间:2013-07-04 14:20:43

标签: excel vba range

我确信这是一个简单的但似乎无法在WWW上找到任何明确的内容。

我需要我的代码来识别列中1到9之间的任何数字,以便将错误返回到该范围,我目前只能使用一个数字。

Public Sub ErrorCheck()
Dim FindString As String
Dim Rng As Range
FindString = ""
If VBA.Trim(FindString) <> "" Then
With Sheets("Scoring").Range("S:S")
    Set Rng = .Find(What:=FindString, _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
    If Not Rng Is Nothing Then
        MsgBox "Error", True
    Else

    End If
End With
End If
End Sub

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以迭代地为所需的所有值执行代码。例如:

Public Sub ErrorCheck()
    Dim FindString As String
    Dim Rng As Range
    Dim startVal As Integer, endVal As Integer

    startVal = 1
    endVal = 9

    For i = startVal To endVal
        FindString = CStr(i)
        With Sheets("Scoring").Range("S:S")
            Set Rng = .Find(What:=FindString, _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
            If Not Rng Is Nothing Then
                MsgBox "Error", True
                Exit For
            Else

            End If
        End With
    Next i

End Sub