我在Windows 7上使用Excel 2010。
我的userform包含一个时间戳,它正在工作,但现在当我将数据保存到工作表时,timedate字段没有重新填充,它是一个设置为“now”的文本字段。我收到了错误错误。
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
答案 0 :(得分: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错了吗?没有。有没有意义吗?是。