【问题标题】:Return data last row vba user form excel返回数据最后一行vba用户表单excel
【发布时间】:2016-02-11 01:53:15
【问题描述】:

我正在制作某种足球数据库,我将在其中使用用户表单输入数据,并且我想从我的 excel 数据库中检索数据。

我有一个名为“wedstrijden”的工作表。 此工作表包含以下列:Date、HomeTeam、AwayTeam、HomeScore、AwayScore、HomeOdds 和 AwayOdds。

我的另一个工作表名为“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

但现在我想使用一个名为“GetData”的按钮将最后一行(工作表“wedstrijden”)的值返回到我的用户窗体,但我不知道如何编写该按钮。

【问题讨论】:

  • 您可以使用SpecialCells Dim lastRow As Long lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row获取最后一行
  • Set ingevenuitslagen = ThisWorkbook.Sheets("wedstrijden") ?你不是说Set ingevenuitslagen = ThisWorkbook.Sheets("ingevenuitslagen") 吗?还是Set wedstrijden = ThisWorkbook.Sheets("wedstrijden")?有点乱^^
  • @MalawiM :我不认为问题在于获得最后一行,因为他使用了NextRow = ingevenuitslagen.Cells(Rows.Count, 1).End(xlUp).Row + 1。顺便说一句 UsedRange 不是解决这类问题(查找数据)的好方法,看看:stackoverflow.com/questions/11169445/…
  • Tnx 供输入人员使用。你能告诉我是否也可以使用 vba 一次收起(或检索)10 行?因此,如果我制作一个可以输入 10 行的用户表单。我可以一次收起这 10 行吗?

标签: vba excel userform


【解决方案1】:

这个按钮看起来很像你已经拥有的东西,像这样:

Private Sub GetData_Click()
Dim wedstrijden As Worksheet
Set wedstrijden = ThisWorkbook.Sheets("wedstrijden")

With wedstrijden
    NextRow = .Cells(.Rows.Count, 1).End(xlUp).Row

    UitslagenIngeven.date_txt.Text = .Cells(NextRow, 1)
    UitslagenIngeven.cboHomeTeam = .Cells(NextRow, 2)
    UitslagenIngeven.cboAwayTeam = .Cells(NextRow, 3)
    UitslagenIngeven.cboHScore = .Cells(NextRow, 4)
    UitslagenIngeven.cboAScore = .Cells(NextRow, 5)
    UitslagenIngeven.hodds_txt.Text = .Cells(NextRow, 6)
    UitslagenIngeven.aodds_txt.Text = .Cells(NextRow, 7)
End With
End Sub

如何使用数据数组

放置在用户窗体中以填充数据数组的代码:

Public DataA() 'This line should be at the top of the module

'Code to Set the dimension of the Data array
Private Sub UserForm_Initialize()
    '5 is the number of information that you want to store
    Dim DataA(5,0) 
    '----Or
    'Take the number of column of your Data Base
    Dim DataA(ThisWorkbook.Sheets("DB").Range("A1").End(xlToRight).Column + 1,0) 

    '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) = Now()
    DataA(2) = Me.Lb_Data.Caption
    DataA(3) = Me.Lb_Year.Caption
    DataA(4) = Me.Lb_BL.Caption
    DataA(5) = Me.Lb_Country

    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

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

Public Sub SetData(ByVal Data_Array As Variant)
Dim DestRg As Range, _
    A()
'Find the last row of your DataBase
Set DestRg = ThisWorkbook.Sheets("DB").Range("Db_Val").Cells(ThisWorkbook.Sheets("DB").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
'Set Increasing ID
ThisWorkbook.Sheets("DB").Cells(Rows.Count, 1).End(xlUp) = ThisWorkbook.Sheets("DB").Cells(Rows.Count, 1).End(xlUp).Offset(-1, 0) + 1
End Sub

取消过滤您正在使用的数据库(这里是 DB 表中的命名范围 Db_Val

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
    Sheets("DB").Activate
    Sheets("DB").Range("A1").Activate
    Sheets("DB").ShowAllData
    DoEvents
    Sheets(ActiveS).Activate
Application.ScreenUpdating = CurrScreenUpdate
End Sub

【讨论】:

  • 是否也可以有一个表单,例如,首先在我的用户表单中放入 10 行,当我按下 PutAway 时,所有这些行都放在我的数据库表中(“韦斯特赖登")
  • 是的,您必须将它们存储到一个数组中(比如说A())并使用.Cells(NextRow,1).Resize(Ubound(A,1),Ubound(A,2)).Value = A 之类的东西将其“打印”到您的工作表中! ;)
  • 你能给我一个代码示例,你将如何做到这一点。因为我是 vba 的新手 :) 并且还在学习
  • @Gerben69 :我编辑了答案以包含我所拥有的内容,您需要对其进行一些调整,但结构就在那里! ;) 顺便说一句,如果您现在可以投票支持我的回答,那就太好了! :) 享受吧! ;)
  • 感谢您所做的一切。现在将尝试使其工作:)我赞成您的回答;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多