1

我想要一个代码,如果工作表中的单元格(其中将传输数据)已经有内容,则用户表单中将出现错误或提示。截至目前,我使用的代码没有显示任何提示,但如果要再次将数据传输到单元格,它会成功地不更新单元格。

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
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

'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

Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub

Private Sub UserForm_Initialize()
'Set Time In as default
TimeOptionButton1.Value = True

'Empty BarcodeTextBox
BarcodeTextBox.Value = ""

Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub

先感谢您!

时间戳用户表单

输出表

4

1 回答 1

0

关于您的代码,我认为您可以添加如下内容:

If TimeOptionButton1.Value = True Then
    if len(cells(emptyRow,5)) = 0 then
        MsgBox "Write Error Message here"
    else
        Cells(emptyRow, 5).Value = "Yes"        
    end if
End If

对于每个是。您可以考虑稍后构建一个单独的函数/子以避免重复。

于 2016-12-16T10:40:04.607 回答