Excel数组值存储

时间:2014-03-31 12:56:29

标签: excel vba excel-vba excel-2007 excel-2010

我目前正在编写一个宏,它是我工作的标签生成器,基本上它有一个组合框,在组合框中你可以选择1-4中的数字(让我们调用这个数字框以备将来参考),那里也是一堆复选框和其他组合框用于选择标签选项。在宏中我有一个数组存储每个数字框的所有信息。每个数字框都有相同的选项可供选择,但所选的选项可能不同。

EXAMPLE:

Numberbox: 1
name label: yes
last name label: no
gender: yes

Numberbox: 2
name label: no
last name label: yes
gender: yes

Numberbox: 4
name label: no
last name label: yes
gender: no

我已经说过,当用户在数字框中选择不同的数字时,它会调用一个子程序,该子程序将所有选定的标签存储到一个数组中。调用此子程序并存储所有信息后,无论用户在数字框中选择了多少数字,它都会将该信息拉出并自动选择用户已选择的选项。

EXAMPLE:

let's say i start fresh, no info submitted into array yet, i select: 

NUMBERBOX: 1
name label: yes
last name label: no
gender: yes

在上面的选择之后我进入数字框并选择数字2,所有信息将首先保存到数组ARRAYS1(1,等等等等)。然后它将调查数组ARRAYS1(2,等等等等)因为我选择了2,检查所选择的选项(在这种情况下是无)并拉出信息。但由于我还没有提交任何信息,选项名称标签,姓氏标签和性别将为空白,如下所示:

NUMBERBOX: 2
name label: 
last name label: 
gender: 

now lets say i input info into numberbox 2

NUMBERBOX: 2
name label: no
last name label: no
gender: no

现在,如果我进入NUMBERBOX并选择1 AGAIN,它会将所有信息存储到数字框2的数组中,所以ARRAYS1(2,等等等等)回到数组中获取数字框1的信息,现在它应该是这样的:

NUMBERBOX: 1
name label: yes
last name label: no
gender: yes

^^^^(the same as chosen from the first time).

现在的问题是我有一个按钮,点击它时会选择其中一个选项并检查它

q24.value=true

让我们说这个复选框是名称标签YES选项,我有另一个按钮,它会改变NUMBERBOX值,让我们说" 2"

Current_Label_Number.Text = "2"

应调用Private Sub current_Label_number_Change()并将信息存储到数组中,然后将其更改为2。

但由于某种原因,它不存储信息,由于某种原因我的选项仅在用户选择复选框或组合框本身时使用以下代码存储:q24.value = true不起作用。我不知道为什么,有人请帮忙!

谢谢

1 个答案:

答案 0 :(得分:0)

这是存储信息和检索信息的完整代码

   Private Sub Current_Bay_Number_Enter()

            Dim i As Integer
            For i = 0 To 19 Step 1

                If Me.Controls("Q" & i).Value = -1 Then
                    Question_Value(Cu

rrent_Bay_Number.Value - 1, i) = 1
            Else
                Question_Value(Current_Bay_Number.Value - 1, i) = Me.Controls("Q" & i).Value
            End If
        Next

        For i = 22 To 27 Step 1
            If Me.Controls("Q" & i).Value = -1 Then
                Question_Value(Current_Bay_Number.Value - 1, i) = 1
            Else
                Question_Value(Current_Bay_Number.Value - 1, i) = Me.Controls("Q" & i).Value
            End If
        Next

        Call DataEntry

    End Sub

'''''''''''&#39 ;''''''''''''& #39;'''''''''''&#39 ;''''''''''''& #39;'''''''''''&#39 ;''''''''''

Public Sub DataEntry()

    Dim temp() As Integer
    ReDim temp(ArraySize)
    Dim i As Integer

    For i = 0 To 19 Step 1

        If Me.Controls("Q" & i).Value * (-1) <> 0 Then
        '//CSA STANDARD
            If i = 0 Then
                temp(2) = temp(2) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//CSA SPECIAL
            If i = 1 Then
                temp(3) = temp(3) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//MINI RUPTER SWITCH
            If i = 2 Then
                temp(9) = temp(9) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//ALDUTI SWITCH
            If i = 3 Then
                temp(9) = temp(9) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//ACCESS PLATE INTERIOR
            If i = 4 Then
                temp(15) = temp(15) + Me.Controls("Q" & i).Value
                temp(12) = temp(12) + Me.Controls("Q" & i).Value
            End If
        '//DOOR
            If i = 5 Then
                temp(10) = temp(10) + Me.Controls("Q" & i).Value
                temp(17) = temp(17) + Me.Controls("Q" & i).Value
            End If
        '//PANEL(NON UTILITY)
            If i = 6 Then
                temp(10) = temp(10) + Me.Controls("Q" & i).Value
                temp(12) = temp(12) + Me.Controls("Q" & i).Value
                temp(17) = temp(17) + Me.Controls("Q" & i).Value
            End If
        '//SCREEN DOOR
            If i = 7 Then
                temp(15) = temp(15) + Me.Controls("Q" & i).Value
                temp(12) = temp(12) + Me.Controls("Q" & i).Value
            End If

        '//SERVICE ENTRANCE
            If i = 9 Then
                temp(14) = temp(14) + Me.Controls("Q" & i).Value * (-1)
                temp(16) = temp(16) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//NUMBER OF HEATERS
            If i = 10 Then
                temp(18) = temp(18) + Me.Controls("Q" & i).Value
            End If

        '//VOLTAGE INDICATORS
            If i = 12 Then
                temp(20) = temp(20) + Me.Controls("Q" & i).Value
            End If
        '//FUSE REFILL
            If i = 13 Then
                temp(21) = temp(21) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//INSTRUCTION LABEL - 1 PER LINEUP, NOT INCLUDED HERE

        '//ACCESS PLATE EXTERIOR (USED TO BE PT CARIAGE)
            If i = 14 Then
                temp(10) = temp(10) + Me.Controls("Q" & i).Value
                temp(12) = temp(12) + Me.Controls("Q" & i).Value
            End If
        '//SMD
            If i = 15 Then
                temp(26) = temp(26) + Me.Controls("Q" & i).Value * (-1)
                temp(27) = temp(27) + Me.Controls("Q" & i).Value * (-1)
                temp(29) = temp(29) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//FUSE ABOVE SWITCH
            If i = 16 Then
                temp(23) = temp(23) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//DOUBLE BREAK SWITCH
            If i = 17 Then
                temp(28) = temp(28) + Me.Controls("Q" & i).Value * (-1)
            End If
        '//46KV
            If i = 18 Then
                temp(9) = temp(9) * 3
                temp(23) = temp(23) * 3
                temp(29) = temp(29) * 3
            End If

        '//Instruction label and manual location
            If i = 19 Then
                temp(22) = temp(22) + Me.Controls("Q" & i).Value * (-1)
            End If

        End If
    Next

    For i = 22 To ControlCount Step 1
        If Me.Controls("Q" & i).Value * (-1) <> 0 Then
            '//SERVICE ENTRANCE BAY
                If i = 22 Then
                    temp(11) = temp(11) + Me.Controls("Q" & i).Value * (-1)
                End If
            '//LINE/LOAD SEPARATOR
                If i = 23 Then
                    temp(7) = temp(7) + Me.Controls("Q" & i).Value * (-1)
                    temp(8) = temp(8) + Me.Controls("Q" & i).Value * (-1)
                End If
             '//TIE BAYS
                If i = 24 Then
                    temp(16) = temp(16) + Me.Controls("Q" & i).Value * (-1)
                End If
             '//WIRE TROUGH
                If i = 25 Then
                    temp(33) = temp(33) + Me.Controls("Q" & i).Value * (-1)
                    'Bays(Current_Bay_Number.Value - 1, 33) = Bays(Current_Bay_Number.Value - 1, 33) + Me.Controls("Q" & i).Value * (-1)
                End If
             '//INCOMER
                If i = 26 Then
                    temp(16) = temp(16) + Me.Controls("Q" & i).Value * (-1)
                End If
             '//# OF PANELS(UTILITY)
                If i = 27 Then
                    temp(13) = temp(13) + Me.Controls("Q" & i).Value
                    temp(10) = temp(10) + Me.Controls("Q" & i).Value
                    temp(17) = temp(17) + Me.Controls("Q" & i).Value
                End If

        End If
    Next

    '//Storing bay information array into master array
    For i = 0 To ArraySize Step 1
        Bays(Current_Bay_Number.Value - 1, i) = temp(i)
    Next
End Sub

&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39 ;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;& #39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39 ;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;& #39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39 ;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;& #39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;

Private Sub current_bay_number_Change()

    Dim i As Integer

    For i = 0 To 19 Step 1

        Me.Controls("Q" & i).Value = Question_Value(Current_Bay_Number.Value - 1, i)
        If Me.Controls("Q" & i).Value = -1 Then
            Me.Controls("Q" & i).Value = 1
        End If
    Next

    For i = 22 To 27 Step 1
            Me.Controls("Q" & i).Value = Question_Value(Current_Bay_Number.Value - 1, i)
        If Me.Controls("Q" & i).Value = -1 Then
            Me.Controls("Q" & i).Value = 1
        End If
    Next

    '"SMD-2C FUSE" & "DOUBLE BREAK SWITCH" & "# OF DOORS" combo box is locked
    Q15.Locked = True
    Q17.Locked = True

End Sub