0

我正在使用 vba 代码来检查 criterium1 保留范围内的单元格。然后我需要遍历其他范围以检查其他标准2、3、4 ...我的范围必须是可变的。它逐列循环,总是在第 3 行和“lr”之间,它是一个变量。我还有一个变量“col”的列号,因为这是我必须递增循环的变量。我写了这个,但它不起作用。在我的 For Each 循环中,我还调用了其他函数。这些函数称为“CriteriaCol1”、“CriteriaCol2”等。所以 Sub im 调用中的列号和编号是相同的。那么有没有办法让副标题中的数字也成为变量?这是一些令人困惑的代码:

Sub error_rept()

Dim lr As Long
Dim colrg As Range
Dim col As Integer
Dim Passed As Boolean

    lr = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.Count - 1, 0).End(xlUp).Row - 1
    col = 1

    Do While col < 8

        set colrg=sheets("NewDataSheet").range(col,3:col,lr)
            For Each cell In colrg
                '##if "run CriteriaCol1 and check if value of passed is true or false" then
                    Next
                Else
                    'do something
                End If
            Next

End Sub

Sub CriteriaCol()

Dim passes As Boolean

    If (cell.Value) = 6.01 Or (cell.Value) = 6.03 Or (cell.Value) = 6.04 Or (cell.Value) = 6.27 Then
        Passed = True
    Else
        Passed = False
    End If

End Sub

Sub CriteriaCol2()

Dim Passed As Boolean

    If (cell.Value) < 9999 And (cell.Value) > 1000 Then
        Passed = True
    Else
        Passed = False
    End If
End Sub

行 set colrg 给出错误:语法错误

有什么建议么?谢谢

4

2 回答 2

0

One way of setting a range is by using the Cells() method. To set a range object to be "B2:C5", for example, you'd use something like the following:

set colrg = Sheets("Sheet1").Range(Cells(2,2),Cells(5,3))

Since (2, 2) corresponds to the second row and second column, B2 and (5, 3) corresponds to the fifth row and third column, C5.

You can then loop through this range using a For Each with a variant or cell object.

于 2013-07-12T14:45:57.357 回答
0

尝试这个 ...

set colrg=sheets("NewDataSheet").range(format(col) & "3:" & format(col) & format(lr))
于 2013-07-12T14:53:44.670 回答