IF语句VBA中变量的重复删除

时间:2016-08-07 21:18:59

标签: arrays vba excel-vba if-statement excel

所以我试图在IF语句中声明一个数组,它将根据条件改变它的大小,但是当我去运行代码时,我不断收到重复的错误。

    If ZoneComboBox.Value = "Zone 1" Then
        Dim spaces(1 To 5, 1 To 2) As String
        spaces(1, 1) = "ROOM 1"    
        spaces(1, 2) = "JOE"
        spaces(2, 1) = "ROOM 2"    
        spaces(2, 2) = "PAUL"
        spaces(3, 1) = "ROOM 3"    
        spaces(3, 2) = "JOE"
        spaces(4, 1) = "ROOM 4"    
        spaces(4, 2) = "PAUL"
        spaces(5, 1) = "ROOM 5"    
        spaces(5, 2) = "PAUL"
    End If
    If ZoneComboBox.Value = "Zone 2" Then
        Dim spaces(1 To 6, 1 To 2) As String
        spaces(1, 1) = "SPACE 1"    
        spaces(1, 2) = "JOE"
        spaces(2, 1) = "SPACE 2"    
        spaces(2, 2) = "JOE"
        spaces(3, 1) = "SPACE 3"    
        spaces(3, 2) = "JOE"
        spaces(4, 1) = "SPACE 4"    
        spaces(4, 2) = "JOE"
        spaces(5, 1) = "SPACE 5"    
        spaces(5, 2) = "JOE"
        spaces(6, 1) = "SPACE 6"    
        spaces(6, 2) = "JOE"
    End if

在我添加第二维以添加谁拥有房间/空间之前它正在工作但我没有看到我如何做这个错误。感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以做的是将If块上方的空格变暗,并在IF块中使用Redim spaces(...)

但是,为什么在比较同一个字段时使用单独的IF块?下面以更简洁的方式做你想要的:

Option Explicit

Sub SO38818662(Z As String)
    Dim spaces() As String
    Select Case Z
        Case "Zone 1"
            ReDim spaces(1 To 5, 1 To 2)
            spaces(1, 1) = "ROOM 1"
            spaces(1, 2) = "JOE"
            spaces(2, 1) = "ROOM 2"
            spaces(2, 2) = "PAUL"
            spaces(3, 1) = "ROOM 3"
            spaces(3, 2) = "JOE"
            spaces(4, 1) = "ROOM 4"
            spaces(4, 2) = "PAUL"
            spaces(5, 1) = "ROOM 5"
            spaces(5, 2) = "PAUL"
        Case "Zone 2"
            ReDim spaces(1 To 6, 1 To 2)
            spaces(1, 1) = "SPACE 1"
            spaces(1, 2) = "JOE"
            spaces(2, 1) = "SPACE 2"
            spaces(2, 2) = "JOE"
            spaces(3, 1) = "SPACE 3"
            spaces(3, 2) = "JOE"
            spaces(4, 1) = "SPACE 4"
            spaces(4, 2) = "JOE"
            spaces(5, 1) = "SPACE 5"
            spaces(5, 2) = "JOE"
            spaces(6, 1) = "SPACE 6"
            spaces(6, 2) = "JOE"
    End Select
End Sub