如何从UserForm一次添加多个数据行到Excel数据库

我正在做一些足球数据库,我会input数据使用用户窗体,我想从我的Excel数据库中检索数据。

我有一个工作表名为:“wedstrijden”这个工作表包含列:date,HomeTeam,AwayTeam,HomeScore,AwayScore,HomeOdds和AwayOdds

我的其他工作表被命名为:“ingevenuitslagen”这个工作表包含我的用户表单称为UitslagenIngeven

使用下面的代码,我能够从我的数据input到我的“wedstrijden”工作表

Private Sub putAway_Click() Dim ingevenuitslagen As Worksheet Set ingevenuitslagen = ThisWorkbook.Sheets("wedstrijden") NextRow = ingevenuitslagen.Cells(Rows.Count, 1).End(xlUp).Row + 1 ingevenuitslagen.Cells(NextRow, 1) = CDate(date_txt.Text) ingevenuitslagen.Cells(NextRow, 2) = UitslagenIngeven.cboHomeTeam ingevenuitslagen.Cells(NextRow, 3) = UitslagenIngeven.cboAwayTeam ingevenuitslagen.Cells(NextRow, 4) = UitslagenIngeven.cboHScore ingevenuitslagen.Cells(NextRow, 5) = UitslagenIngeven.cboAScore ingevenuitslagen.Cells(NextRow, 6) = Val(UitslagenIngeven.hodds_txt.Text) ingevenuitslagen.Cells(NextRow, 7) = Val(UitslagenIngeven.aodds_txt.Text) End Sub 

但是这只是放一行。 我希望能够一次收起10行或15行。 所以我会做一个用户表单的可能性,把20行,但它应该能够只收回填充行。

这可能吗? 我应该如何调整我的用户表单? 我可以复制文本和combobox区域吗?

如何使用数据数组

你需要创build一个新的button,你会有:

  1. 一个用于将数据集添加到数据数组 (这里是CommandButton1 )和
  2. 一个将数据数组添加到数据库 (这里是CommandButton2 )。

我也更喜欢使用数据库的命名范围 ,在这里它被称为Db_Val但您可以重新命名,以满足您的需求! ;)

放置在UserForm中以填充数据数组的代码:

 Public ingevenuitslagen As Worksheet Public DataA() '----These lines should be at the top of the module '----Code to Set the dimension of the Data array Private Sub UserForm_Initialize() Dim DataA(7, 0) Set ingevenuitslagen = ThisWorkbook.Sheets("wedstrijden") '----Rest of your code End Sub '----Code to add a data set to the data array Private Sub CommandButton1_Click() UnFilter_DB '----See below procedure DataA(1) = CDate(date_txt.Text) DataA(2) = UitslagenIngeven.cboHomeTeam DataA(3) = UitslagenIngeven.cboAwayTeam DataA(4) = UitslagenIngeven.cboHScore DataA(5) = UitslagenIngeven.cboAScore DataA(6) = Val(UitslagenIngeven.hodds_txt.Text) DataA(7) = Val(UitslagenIngeven.aodds_txt.Text) ReDim Preserve DataA(LBound(DataA, 1) To UBound(DataA, 1), LBound(DataA, 2) To UBound(DataA, 2) + 1) End Sub '----Code to sent the data array to the DB Private Sub CommandButton2_Click() ReDim Preserve DataA(LBound(DataA, 1) To UBound(DataA, 1), LBound(DataA, 2) To UBound(DataA, 2) - 1) SetData DataA End Sub 

在数据库中打印从用户表单传递的数据数组的过程:

这里的数据库是ingevenuitslagen表中的命名范围Db_Val

 Public Sub SetData(ByVal Data_Array As Variant) Dim DestRg As Range, _ A() '----Find the last row of your DataBase Set DestRg = ingevenuitslagen.Range("Db_Val").Cells(ingevenuitslagen.Range("Db_Val").Rows.Count, 1) '----Print your array starting on the next row DestRg.Offset(1, 0).Resize(UBound(Data_Array, 1), UBound(Data_Array, 2)).Value = Data_Array End Sub 

删除你正在使用的数据库:

 Public Sub UnFilter_DB() '----Use before "print" array in sheet to unfilter DB to avoid problems (always writing on the same row if it is still filtered) Dim ActiveS As String, CurrScreenUpdate As Boolean CurrScreenUpdate = Application.ScreenUpdating Application.ScreenUpdating = False ActiveS = ActiveSheet.Name ingevenuitslagen.Activate ingevenuitslagen.Range("A1").Activate ingevenuitslagen.ShowAllData DoEvents Sheets(ActiveS).Activate Application.ScreenUpdating = CurrScreenUpdate End Sub 

美好的一天。

我也有同样的挑战 我的是能够放置一个客户的订单。 有了我的代码,我只能为客户每次下单一个产品。 我希望能够在一个用户窗体中同时为一个客户的每个订单放置多个产品,并且它将更新多行。 下面的代码只能为一个客户连续更新一行产品:

 Private Sub cmdAdd_Click() Dim lRow As Long Dim ws As Worksheet lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row With ws .Cells(lRow, 1).Value = Me.Data1.Value .Cells(lRow, 2).Value = Me.Data2.Value .Cells(lRow, 3).Value = Me.Data3.Value .Cells(lRow, 4).Value = Me.Data4.Value .Cells(lRow, 5).Value = Me.Data5.Value .Cells(lRow, 6).Value = Me.Data6.Value .Cells(lRow, 7).Value = Me.Data7.Value .Cells(lRow, 8).Value = Me.Data8.Value .Cells(lRow, 9).Value = Me.Data9.Value .Cells(lRow, 10).Value = Me.Data10.Value End With End Sub 

以上只能更新每个客户的一个产品。 客户可以订购多个产品。