简体   繁体   中英

VBA Excel 2013: Assign Array Values from Another UserForm

I'm relatively new to VBA. I have a program I'm writing where the user is given the option to change their input from a 2 dimensional array in another user form.

The first user form, UserForm1, allows the user to input the information from text fields and saves it to the respective array row, i, when pressing a Save command button.

When the user presses an OK command button, the user is asked if they want to add another set of data. If they say no, they are asked if they want to change data. If they say yes, then another user form, UserForm2, is opened.

The code for UserForm1 is similar to the code below:

    Public MyArray as Variant, i as Integer

    Sub Userform_Initialize()
        ReDim MyArray(100,4)
    End Sub

    Sub SaveButton_click()
        MyArray(i, 1) = TextField1.Value
        MyArray(i, 2) = TextField2.Value
        MyArray(i, 3) = TextField3.Value
        MyArray(i, 4) = TextField4.Value
    End Sub

    Sub OKButton_click()
        If msgbox("Do you want to add more data?", vbYesNo) = vbNo Then
            If msgbox("Do you have corrections to be made?",vbYesNo) = vbYes Then
                Load UserForm2
                UserForm2.Show
            Else:  Exit Sub
            End If
        Else:  i = i + 1
            Exit Sub
        End If
    End Sub

In UserForm2, the user chooses the row number, i, from a combo box. When the row number is selected, the array information is automatically populated in text fields from UserForm1.

When the user presses the Save command button, it should pass the information from the text fields and write it to the respective row.

The code for UserForm2 is similar to the code below:

    Public j as integer

    Sub Userform_Initialize()
        For j = 1 to UserForm1.i
            ComboBox1.AddItem (j)
        Next
    End Sub

    Sub SaveButton_click()
        UserForm1.MyArray(ComboBox1.Value, 1) = TextField1.Value
        UserForm1.MyArray(ComboBox1.Value, 2) = TextField2.Value
        UserForm1.MyArray(ComboBox1.Value, 3) = TextField3.Value
        UserForm1.MyArray(ComboBox1.Value, 4) = TextField4.Value
    End Sub

Stepping through the code, the values from MyArray should be properly referenced, and I can see the values initially saved from UserForm1. However, the values are not changing as I step to the next line.

Does anyone have a solution for my problem? Thank you in advance for your help!

I believe I found my solution. I had to declare the array in the module containing the code to start the program as a public variable. After I did that and modified the code, the values were written properly to the array.

If anyone has other solutions, though, I would like to know. I'm not that experienced with VBA, so I want to hear other solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM