0

我在 Windows 7 上使用 Excel 2010。

我的用户表单包含一个时间戳,它正在工作,但是现在当我将数据保存在工作表中时,时间日期字段没有重新填充,它是一个设置为“现在”的文本字段。我收到不匹配错误。

Private Sub UserForm_Initialize()


TimeDate.Value = Now
TimeDate = Format(TimeDate.Value, "mmmm dd yyyy hh:mm")
City.List = Worksheets("Sheet2").Range("A2:A10").Value
State.List = Worksheets("Sheet2").Range("B2:B3").Value
Insurance.List = Worksheets("Sheet2").Range("C2:C7").Value
EBCAPServices.List = Worksheets("Sheet2").Range("D2:D3").Value
Patient.List = Worksheets("Sheet2").Range("D2:D3").Value
SelfEnroll.List = Worksheets("Sheet2").Range("D2:D3").Value
ContactType.List = Worksheets("Sheet2").Range("G2:G5").Value
Staff.List = Worksheets("Sheet2").Range("H2:H14").Value


End Sub


Private Sub CancelButton_Click()
Unload Me
End Sub


Private Sub ClearButton_Click()


Dim ctl As Control


For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl
End Sub


Private Sub OKbutton_Click()


Dim RowCount As Long
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count


With Worksheets("Sheet1").Range("A1")
.Offset(RowCount, 0).Value = DateValue(Me.TimeDate.Value)
.Offset(RowCount, 1).Value = Me.FirstName.Value
.Offset(RowCount, 2).Value = Me.LastName.Value
.Offset(RowCount, 3).Value = Me.Address1.Value
.Offset(RowCount, 4).Value = Me.Address2.Value
.Offset(RowCount, 5).Value = Me.City.Value
.Offset(RowCount, 6).Value = Me.State.Value
.Offset(RowCount, 7).Value = Me.ZipCode.Value
.Offset(RowCount, 8).Value = Me.Phone1.Value
.Offset(RowCount, 9).Value = Me.Phone2.Value
.Offset(RowCount, 10).Value = Me.Insurance.Value
.Offset(RowCount, 11).Value = Me.EBCAPServices.Value
.Offset(RowCount, 12).Value = Me.Patient.Value
.Offset(RowCount, 13).Value = Me.Income.Value
.Offset(RowCount, 14).Value = Me.FamilySize.Value
.Offset(RowCount, 15).Value = Me.SelfEnroll.Value
.Offset(RowCount, 16).Value = Me.SocialMedia.Value
.Offset(RowCount, 17).Value = Me.ContactType.Value
.Offset(RowCount, 18).Value = Me.Staff.Value
.Offset(RowCount, 19).Value = Me.Notes.Value




.Offset(RowCount, 0).Value = Format(Now, "mmmm dd yyyy hh:mm")
End With


If Staff.Value = "" Then
MsgBox "Please choose your initials."
Exit Sub
End If


If ContactType.Value = "" Then
MsgBox "Please choose the type of contact made."
Exit Sub
End If


If FirstName.Value = "" Then
MsgBox "Please enter a First Name."
FirstName.SetFocus
Exit Sub
End If


If LastName.Value = "" Then
MsgBox "Please enter a Last Name."
LastName.SetFocus
Exit Sub
End If


If Phone1.Value = "" Then
MsgBox "Please enter the phone number."
Phone1.SetFocus
Exit Sub
End If


Dim ctl As Control


For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl




End Sub


Private Sub OnlyNumbers()


If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
End If
End With
End If
End Sub


Private Sub FamilySize_Change()
OnlyNumbers
End Sub


Private Sub Income_Change()
OnlyNumbers
End Sub


Private Sub Phone1_Change()
OnlyNumbers
End Sub


Private Sub Phone2_Change()
OnlyNumbers
End Sub


Private Sub ZipCode_Change()
OnlyNumbers
End Sub
4

1 回答 1

1

我无法发表评论,因为我没有 50 的代表,但我测试了它并且它对我有用。我确实看到的一件事是这条线

.Offset(RowCount, 0).Value = Format(Now, "mmmm dd yyyy hh:mm")

使这条线

.Offset(RowCount, 0).Value = DateValue(Me.TimeDate.Value)

无用。格式行从 Now() 函数获取日期和时间,而不是从字段中已有的内容,代码没有问题,但也许在那里查看你的逻辑。

这就像在说:

dim x as integer

x = 0

x = 1

x = 0 错了吗?不,在那里没有意义吗?是的。

于 2013-08-19T15:30:37.210 回答