我正在制作某种足球数据库,我可以在其中使用用户表单输入数据,并希望从 Excel 数据库中检索数据。
我有一个名为:“wedstrijden”的工作表 该工作表包含以下列:日期、主队、客队、主场得分、客场得分、主场赔率和客场赔率
我的另一个工作表名为:“ingevenuitslagen”此工作表包含我名为 UitslagenIngeven 的用户表单
使用下面的代码,我可以将数据从用户表单输入到我的“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
但这只是收起1行。我希望能够一次收起 10 或 15 行。因此,我将创建一个可以存放 20 行的用户表单,但它应该只能存放那些已填充的行。
这可能吗?我应该如何调整我的用户表单?我可以只复制文本和组合框区域吗?
最佳答案
如何使用数据数组
您需要创建一个新按钮,您将拥有:
- 用于将数据集添加到数据数组(此处为
CommandButton1
)和 - 用于将数据数组添加到数据库(此处为
CommandButton2
)。
我也更喜欢使用数据库的命名范围,这里称为Db_Val
,但您可以重命名它以满足您的需要! ;)
放置在用户窗体中以填充数据数组的代码:
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
关于arrays - 如何一次从用户窗体添加多个数据行到 Excel 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627391/