集合值意外地更改为集合中的最高值

时间:2018-05-07 11:18:49

标签: vba excel-vba excel

我有一个验证函数,它在作为参数给出的范围上运行验证。

我有一个类,它是一个具有行号和列号的验证对象。

如果有一个空白单元格,则会将细胞的详细信息添加到 ValidationError对象,然后添加到集合中。

但是,集合的值会在函数末尾发生变化

以下是验证码

Option Explicit

Public Sub Test()

    CellValidation (Sheets(1).range("A1:C11"))

End Sub

Public Function CellValidation(range As range)
    Dim Row As Long
    Dim sheet As Worksheet
    Dim Column As Long
    Dim arraySize As Long
    Dim l_currentRow As Long
    Dim l_boolRowInvalid As Boolean
    Dim l_validationError As New ValidationError
    Dim l_validationErrors As New Collection
    Dim l_error As Variant

    l_boolRowInvalid = False
    l_currentRow = 1
    arraySize = 0
    Set sheet = range.Worksheet

    With sheet
        'Loop through rows
        For Row = 1 To 11
            'Loop through columns
            For Column = 1 To 3
                With range

                    'Check for blank or null cell
                    If VarType(.Cells(Row, Column).Value) = 0 Or VarType(.Cells(Row, Column).Value) = 1 Then
                        l_boolRowInvalid = True
                    End If

                End With

                If l_boolRowInvalid = True Then

                    l_validationError.Row = Row
                    l_validationError.Column = Column

                    l_validationErrors.Add l_validationError

                    l_boolRowInvalid = False

                    arraySize = arraySize + 1
                    'If I debug here I get my expected values
                    Debug.Print l_validationErrors(arraySize).Row & "," & l_validationErrors(arraySize).Column & " "
                End If

            Next Column
                l_currentRow = l_currentRow + 1
                DoEvents
        Next Row

    End With
    'However when I check the values of the collection here the values have all changed
    Debug.Print "Second Test"
    For Each l_error In l_validationErrors
        Debug.Print l_error.Row & "," & l_error.Column & " "
    Next

End Function

以下是ValidationError类

Public Row As Long
Public Column As Integer

以下是excel表

Excel sheet

以下是该计划提供的输出:

Output

1 个答案:

答案 0 :(得分:0)

尝试此代码怎么样

Public Function CellValidation(range As range)
    Dim Row As Long
    Dim sheet As Worksheet
    Dim Column As Long
    Dim arraySize As Long
    Dim l_currentRow As Long
    Dim l_boolRowInvalid As Boolean
    Dim l_validationError As ValidationError
    Dim l_validationErrors As New Collection
    Dim l_error As Variant

    l_boolRowInvalid = False
    l_currentRow = 1
    arraySize = 0
    Set sheet = range.Worksheet

    With sheet
        'Loop through rows
        For Row = 1 To 11
            'Loop through columns
            For Column = 1 To 3
                With range

                    'Check for blank or null cell
                    If VarType(.Cells(Row, Column).Value) = 0 Or VarType(.Cells(Row, Column).Value) = 1 Then
                        l_boolRowInvalid = True
                    End If

                End With

                If l_boolRowInvalid = True Then
                    Set l_validationError = New ValidationError
                    l_validationError.Row = Row
                    l_validationError.Column = Column

                    l_validationErrors.Add l_validationError

                    l_boolRowInvalid = False

                    arraySize = arraySize + 1
                    'If I debug here I get my expected values
                    Debug.Print l_validationErrors(arraySize).Row & "," & l_validationErrors(arraySize).Column & " "
                End If

            Next Column
                l_currentRow = l_currentRow + 1
                DoEvents
        Next Row

    End With
    'However when I check the values of the collection here the values have all changed
    Debug.Print "Second Test"
    For Each l_error In l_validationErrors
        Debug.Print l_error.Row & "," & l_error.Column & " "
    Next

End Function

您需要在for循环中创建一个新的l_validationError实例,否则将只创建一个l_validationError实例,并且您只更改此实例。