执行case语句后数组值消失VBA(excel)

问题描述 投票:0回答:0

我正在尝试将一组单元格的背景颜色保存在二维数组中,以便在触发“案例 1 到 2”时,单元格背景颜色应存储在 WaferArr 中。问题是,WaferArr 值在“Case 1 To 2”执行后消失(所有值都返回 0)。任何关于为什么会发生这种情况的想法都将受到赞赏。

Private Sub Workbook_Open()
    
    Range("A1:F22").Interior.ColorIndex = 2

End Sub

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim k As Integer
    Dim WaferArr(21, 5) As Integer
    
    k = 13
    
    'If Target.Cells.count > 1 Then Exit Sub
    If IsNumeric(Target) And Target.Address = "$I$6" Then
        Select Case Target.Value
        Case 1 To 2:
            For i = 0 To 5
                For j = k To 21
                    WaferArr(j, i) = Cells(j + 1, i + 1).Interior.ColorIndex
                Next j
                
                If i = 2 Then
                    k = 0
                End If
            Next i
            
            Range("D1:F22").Interior.ColorIndex = 1
            Range("A14:C22").Interior.ColorIndex = 1
        Case Is < 1
            For i = 0 To 5
                For j = k To 21
                    Cells(j + 1, i + 1).Interior.ColorIndex = WaferArr(j, i)
                Next j
                
                If i = 2 Then
                    k = 0
                End If
            Next i
        End Select
    End If
End Sub

WaferArr 值仍然存储在最后一个End If 之后End Sub 之前。我在“k = 13”之后插入 MsgBox WaferArr(0,0) 并且 WaferArr 总是返回 0。当 "Case Is < 1" 执行时,所有 WaferArr 值都是 0.

arrays excel vba multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.