VBA Excel离开文本框时出错

我想有一个半透明的文本来显示如何inputdate的格式。 添加代码后,我发现如果我点击我的txtA_DateCR文本框,然后单击另一个文本框或命令button,错误将不断提示出来。

'This was the code that works fine before adding the following 2 below. Private Sub txtA_DateCR_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) If Mid(txtA_DateCR.Value, 4, 2) > 12 Then MsgBox "Invalid date, make sure format is (dd/mm/yyyy)", vbCritical txtA_DateCR.Value = vbNullString txtA_DateCR.SetFocus Exit Sub End If dDate = DateSerial(Year(Date), Month(Date), Day(Date)) txtA_DateCR.Value = Format(txtA_DateCR.Value, "dd/mm/yyyy") dDate = txtA_DateCR.Value End Sub 'Supposed to change the textbox values to light grey and have the value of dd/mm/yyyy Private Sub txtA_DateCR_AfterUpdate() With txtA_DateCR If .Text = "" Then .ForeColor = &HC0C0C0 .Text = "dd/mm/yyyy" End If End With End Sub 'When user clicks here, the value of dd/mm/yyyy will disappear and font colour will return back to black Private Sub txtA_DateCR_Enter() With txtA_DateCR If .Text = "dd/mm/yyyy" Then .ForeColor = &H80000008 .Text = "" End If End With End Sub Private Sub UserForm_Initialize() txtA_DateCR.ForeColor = &HC0C0C0 txtA_DateCR.Text = "dd/mm/yyyy" cmdEnter.SetFocus End Sub 

在选择到达日期之前选择抵达日期从抵达日期点击

作为一个总结,我只想要popup错误消息,如果用户错误地inputdate的格式,而不是popup,当我点击离开文本框。