How to add multiple data rows at once from UserForm to Excel DataBase

你。 提交于 2020-07-09 06:10:10

问题


I'm making some sort of football database where I would input data using a userform and where I want to retrieve data from my excel database.

I have a worksheet named: "wedstrijden" This worksheet contain the columns: Date, HomeTeam, AwayTeam, HomeScore,AwayScore, HomeOdds and AwayOdds

My other worksheet is named: "ingevenuitslagen" This worksheet contains my userform called UitslagenIngeven

Using the code below I'm able to input my data from the userform to my "wedstrijden" worksheet

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

But this is only to put away 1 row. I would like to make the possibility to put away 10 or 15 rows at once. So I would make a userform with the possibility to put away 20 rows BUT it should be able to put away only those rows that are filled in.

Is this possible? And how should I adjust my userform? Can I just copy the text and combobox areas ?


回答1:


How to work with a Data Array

You'll need to create a new button, you'll have :

  1. one for adding the data set to the data array (here CommandButton1) and
  2. one to add the data array to the data base (here CommandButton2).

I also prefer to work with a Named Range for the Data Base, here it is called Db_Val but you can rename this to fit your needs! ;)

Code to place in the UserForm to fill the data array :

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

Procedure to print in the database the data array that you pass from the user form :

Here the data base is the Named Range Db_Val in ingevenuitslagen sheet

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

Sub to unfilter the DB you are working with :

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



回答2:


Good day all.

I have this same challenge. Mine is to be able to place a Customer's Orders. With the code I have I can only place one product per order at a time for the customer. I want to be able to place multiple products per order for one customer at the same time in a Userform and it will update multiple rows. The code below can only update one row with one product in a row for one customer:

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

The above can only update One product per customer. A customer could place order for more than one product.



来源:https://stackoverflow.com/questions/33627391/how-to-add-multiple-data-rows-at-once-from-userform-to-excel-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!