将多个用户表单提交存储到一个数组并将该数组存储在 table

Store multiple user form submissions to an array and store the array in a table

我在这里看到了以下问题: 这似乎是我的问题的解决方案。

但是,我在尝试实施上述解决方案时遇到了一些困难。

背景: 我想创建一个用户表单,允许我添加股票信息,例如:市场、代码、货币、价格和股票数量,并将这些添加到 table.

尝试:

这是我设计的用户表单(目前很简单)

我想存储多个提交,然后将所有提交添加到 table。

代码

模块 1

Public ws As Worksheet
Public DataA()

用户表单代码

    '----Code to Set the dimension of the Data array
Private Sub UserForm1_Initialize()
    Dim DataA(5, 0)
    Set ws = ThisWorkbook.Sheets("Portfolio")
    '----Rest of your code
End Sub

'----Code to add a data set to the data array

'Button to add is called cmd_add
'Text Boxes are names: txt_SM, txt_Sy, txt_Cu, txt_Pr and txt_Qu
'These represent the desired headings: Stock Market, Symbol, Currency, Price and Quantity



Private Sub cmd_add_Click()

    DataA(1) = UserForm1.txt_SM
    DataA(2) = UserForm1.txt_Sy
    DataA(3) = UserForm1.txt_Cu
    DataA(4) = UserForm1.txt_Pr
    DataA(5) = UserForm1.txt_Qu

    ReDim Preserve DataA(LBound(DataA, 1) To UBound(DataA, 1), LBound(DataA, 2) To UBound(DataA, 2) + 1)
End Sub

但是,在尝试添加提交后我得到了 "Subscript Out of Range Error"。它似乎是数组 DataA 的问题,但我不确定错误在哪里。我在单独的模块中将其声明为 public 数组,并在初始化步骤中指定了维度。

对于解决此问题和开始工作的任何帮助,我们将不胜感激。

谢谢

cmd_add_Click()中你需要为数组使用2个索引...

DataA(1, 0) = UserForm1.txt_SM

更新

看到你的工作簿后,我建议你把代码改成这样...

Private Sub cmd_add_Click()

    ReDim d(1 To 1, 1 To 5)

    With UserForm1
        d(1, 1) = .txt_SM
        d(1, 2) = .txt_Sy
        d(1, 3) = .txt_Cu
        d(1, 4) = .txt_Pr
        d(1, 5) = .txt_Qu
    End With

    ThisWorkbook.Sheets("Portfolio").[a1].End(xlDown).Offset(1).Resize(, 5) = d

End Sub