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

VBA Programming. How to transfer data from userform to specific rows and columns to a sheet?

我目前正在开发一个替代考勤监控系统作为一项举措。目前,我设计了如下所示的用户表单: Time Stamp Userform

它是这样工作的:

  1. 员工将选择 he/she 将使用的时间戳类型:上班、下班、第一次休息开始、第一次休息结束、第二次休息开始或第二次休息结束.

  2. 然后在人员条码字段输入人员编号。

目前,我有这个代码:

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编码方面的专家,我只是在网上搜索了代码。上面的代码确定了将输入数据的下一个空行。填写用户表单后,结果 sheet 如下所示: Results

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

然而,我想要的是只有 1 行将专用于一个员工编号,以便所有 his/her 时间戳都在 1 行而不是多行中。我希望有人可以帮助我。我想到了一个代码,如果输入的人员条形码已经在 sheet 上,则将对其进行搜索,如果是,则将在同一行输入时间戳。我希望它位于 1 行,以便可以轻松地制定公式,例如计算超支和人数。

非常感谢您的好心人! :)

对您的代码进行最少的更改,尝试更改行

`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