VBA识别范围内的数字



我确信这是一个简单的问题,但似乎在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

谢谢你的帮助!

您可以针对所有想要的值迭代执行代码。示例:

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

最新更新