根据2个Combobox值存储/覆盖/从表中获取数据

时间:2016-09-26 00:51:46

标签: excel vba excel-vba combobox

我正在VBA中创建一个报表工具,用于将数据保存在工作表中,然后当两个组合框被更改(日期和团队)时,用户表单将从工作表中的数据填充自己。基本上,我们的目标是拥有一个用户表单,既可以保存每日报告,也可以通过更改日期和团队来提取旧报告。此功能使团队负责人能够在整个班次中填写报告,并随时进行更改。

这基本上是一个两部分问题,我在buttonclick上需要一个宏,它将搜索2列,日期和团队,(想想4个不同的团队ABCD),如果两者不匹配,则将其保存在最后一行。如果存在团队和日期的组合,我想填充userform中的其他文本框。 I will post a picture of the userform here for info.

编辑:这是不起作用的代码。在userform initialize上自动填充日期。

Private Sub cboL1Team_Change()
Dim i As Integer
Dim finalrow As Integer
Dim ws As Worksheet
Set ws = Sheets("Line 1 Database")
finalrow = ws.Range("A100000").End(xlUp).Row

For i = 2 To FinalRow
    If Cells(i, 1) = cboL1Team.Value Then
        If Cells(i, 2) = cboL1Date.Value Then
            cboL1Product.Value = ws.Cells(i, 3).Value
            cboL1Staffing.Value = ws.Cells(i, 4).Value
            txtL1Pounds.Value = ws.Cells(i, 5).Value
            txtL1Processing.Value = ws.Cells(i, 6).Value
            txtL1Packaging.Value = ws.Cells(i, 7).Value
        End If
    End If
Next i
End Sub

1 个答案:

答案 0 :(得分:0)

在userform控件和工作表之间进行读写时,我希望有一个函数可以根据参数切换读写。这里我使用枚举来使参数更具可读性。

我将查找或创建新行的责任提取到函数FindRow中,该函数返回目标行的范围对象。

将数据读入userform控件时,我使用With FindRow.Columns().Columns(3)类似于使用.Cells(1, 3);我只是觉得它看起来更干净。

将数据写回工作表时,我使用了.Columns("E:F").Value = Array(cboL1Product.Value, ...etc.)。在实践中,我只需要反转读取部分并一次分配值1。

标准代码模块

Option Explicit

Public Enum RecordEnum
    enReadRecord
    enWriteRecord
End Enum

用户窗体

Option Explicit

Private Sub cboL1Date_Change()
    If cboL1Team.ListIndex > -1 Then ReadWriteRecord enReadRecord
End Sub

Private Sub cboL1Team_Change()
    If cboL1Date.ListIndex > -1 Then ReadWriteRecord enReadRecord
End Sub

Private Sub btnSaveSubmitReport_Click()
    ReadWriteRecord enWriteRecord
End Sub

Sub ReadWriteRecord(ReadWrite As RecordEnum)
    With FindRow
        If ReadWrite = enReadRecord Then
            cboL1Product.Value = .Columns(3).Value
            cboL1Staffing.Value = .Columns(4).Value
            txtL1Pounds.Value = .Columns(5).Value
            txtL1Processing.Value = .Columns(6).Value
            txtL1Packaging.Value = .Columns(7).Value
        ElseIf ReadWrite = enWriteRecord Then
            .Columns("A:G").Value = Array(cboL1Team.Value, cboL1Date.Value, cboL1Product.Value, cboL1Staffing.Value, txtL1Pounds.Value, txtL1Processing.Value, txtL1Packaging.Value)
        End If
    End With
End Sub

Function FindRow() As Range
    Dim c As Range, Target As Range
    With Worksheets("Line 1 Database")
        For Each c In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
            If c.Value = cboL1Team.Value And DateValue(c.Offset(0, 1).Value) = DateValue(cboL1Date.Value) Then
                Set Target = c
                Exit For
            End If
        Next

        If Target Is Nothing Then Set Target = .Range("A" & .Rows.Count).End(xlUp).Offset(1)

    End With
    Set FindRow = Target.EntireRow
End Function


Private Sub UserForm_Initialize()

    Dim r As Range
    For Each r In Range("Dates")
        cboL1Date.AddItem r.Text
    Next

End Sub

enter image description here