VBA编程。如何将数据从用户表单传输到特定的行和列到表单?

时间:2016-12-16 01:43:13

标签: vba excel-vba userform time-and-attendance excel

我目前正在研究另一种考勤监控系统作为一项举措。目前,我设计的用户表单如下所示: Time Stamp Userform

它的工作原理如下:

  1. 员工将选择他/她将使用的时间戳类型:时间进入,超时,第一次休息开始,第一次休息结束,第二次休息开始或第二次休息结束。

    < / LI>
  2. 然后人员编号将输入人事条码字段。

  3. 目前,我有这段代码:

    Private Sub CancelButton_Click()
        Unload Me
    End Sub
    
    Private Sub ClearButton_Click()
        Call UserForm_Initialize
    End Sub
    
    Private Sub OKButton_Click()
        Dim emptyRow As Long
    
        'Make Sheet1 active
        Sheet1.Activate
    
        'Determine emptyRow
        emptyRow = WorksheetFunction.CountA(Range("B:B")) + 1
    
        'Transfer information
    
        If TimeOptionButton1.Value = True Then
            Cells(emptyRow, 5).Value = "Yes"
        End If
        If TimeOptionButton2.Value = True Then
            Cells(emptyRow, 7).Value = "Yes"
        End If
        If BreakOptionButton1.Value = True Then
            Cells(emptyRow, 9).Value = "Yes"
        End If
        If BreakOptionButton2.Value = True Then
            Cells(emptyRow, 11).Value = "Yes"
        End If
        If BreakOptionButton3.Value = True Then
            Cells(emptyRow, 14).Value = "Yes"
        End If
        If BreakOptionButton4.Value = True Then
            Cells(emptyRow, 16).Value = "Yes"
        End If
    
        Cells(emptyRow, 2).Value = BarcodeTextBox.Value
    End Sub
    
    Private Sub UserForm_Initialize()
    'Set Time In as default
        TimeOptionButton1.Value = True
    
        'Empty BarcodeTextBox
        BarcodeTextBox.Value = ""
    End Sub
    

    我不是VBA编码的专家,我只是在线搜索代码。上面的代码确定将输入数据的下一个空行。填写用户表单后,表单中的结果如下所示: Results

    如您所见,1名员工的时间戳输入多行。

    然而,我想要的只是一行将专用于一个员工编号,以便他/她的所有时间戳都在一行而不是多行。我希望有人可以帮助我。我想到了一个代码,其中如果输入的人员条形码已经在纸张上,则将搜索输入的人员条形码,如果是,则时间戳将被输入到同一行。我希望它在一行上,以便公式可以轻松制定,例如计算过度破坏和人数。

    非常感谢你为善良的撒玛利亚人做好准备! :)

1 个答案:

答案 0 :(得分:2)

只需对代码进行最少的更改,请尝试更改行

`emptyRow = WorksheetFunction.CountA(Range("B:B")) + 1`

进入这个:

Dim rFound as Range: Set rFound = Range("B:B").Find(BarcodeTextBox.Value, , , xlWhole)
If rFound Is Nothing then
    emptyRow = Range("B" & Rows.Count).End(xlUp).Row + 1
Else
    emptyRow = rFound.Row
End If