VBA编程。若工作表中的字段已包含内容,则Userform中将出现错误或提示
我希望有一个代码,如果表单中的单元格(数据将被传输的单元格)已经有内容,那么userform中会出现错误或提示。到目前为止,我使用的代码没有显示任何提示,但是如果要再次向单元格传输数据,它将成功地不更新单元格VBA编程。若工作表中的字段已包含内容,则Userform中将出现错误或提示,vba,excel,prompt,time-and-attendance,Vba,Excel,Prompt,Time And Attendance,我希望有一个代码,如果表单中的单元格(数据将被传输的单元格)已经有内容,那么userform中会出现错误或提示。到目前为止,我使用的代码没有显示任何提示,但是如果要再次向单元格传输数据,它将成功地不更新单元格 Private Sub CancelButton_Click() Unload Me End Sub Private Sub ClearButton_Click() Call UserForm_Initialize End Sub Private Sub OKButton_Click(
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
提前谢谢你
关于您的代码,我认为您可以添加如下内容:
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
每一个答案都是肯定的。你可以考虑建立一个单独的函数/子函数来避免重复。 关于你的代码,我想你可以添加这样的东西:
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
每一个答案都是肯定的。你可以考虑建立一个单独的函数/子函数来避免重复。你好,谢谢你的评论:但是,代码没有编译。如果TimeOptionButton1.Value=True,则[如果len(单元格(emptyRow,5)=0,则]-此部分有问题。这些文本在编码后变为红色。MsgBox“在此处写入错误消息”其他单元格(emptyRow,5)。Value=“是”如果结束如果结束如果抱歉我真的不是专家。我明白了!只是缺少一个右括号。非常感谢!答案中添加了括号:)如果你喜欢答案,请随时标记为已接受,并在可以的时候给它+1:)嗨,谢谢你的评论:)但是,代码没有编译。如果TimeOptionButton1.Value=True,则[如果len(单元格(emptyRow,5)=0,则]-此部分有问题。这些文本在编码后变为红色。MsgBox“在此处写入错误消息”其他单元格(emptyRow,5)。Value=“是”如果结束如果结束如果抱歉我真的不是专家。我明白了!只是缺少一个右括号。非常感谢!答案中添加了括号:)如果你喜欢答案,请随时标记为已接受,并在可以时给出+1:)