我正在处理一个Excel用户窗体,并且除一段代码外,所有代码均正常工作。我将在下面附加代码,但我希望这是一个简单的解决方法。如您在我的代码中看到的那样,当用户输入出发时间和到达时间时,数据将进入我的数据库,并执行计算,然后将其发送回我的用户表单复选框。虽然这可以根据需要进行,但是我发现,如果用户在离开用户表单之前的某一时间进行了更改,则该信息会在我的数据库中正确更新,但是用户表单复选框不会被更新。有人可以告诉我我需要做些什么来使它们实时工作并随着我的数据库文档进行更改吗?
Private Sub txtDepartTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'When time is entered, time transfers immediately to spreadsheet time calculations datafield.
Dim TargetRow As Long
TargetRow = Sheets("Codes").Range("D43").Value + 1
If Not txtDepartTime Like "##:## [ap]m" Then
MsgBox "Time entered is not valid. Please enter time as hh:mm am/pm.", vbExclamation
Cancel = True
Exit Sub
End If
With Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 25)
.Value = TimeValue(txtDepartTime)
.NumberFormat = "hh:mm" 'departure time for checkbox calculation
End With
End Sub
Private Sub txtArrivalTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'When time is entered, time transfers immediately to spreadsheet datafield and sends back to userform which meals are allowed.
Dim TargetRow As Long
TargetRow = Sheets("Codes").Range("D43").Value + 1
If Not txtArrivalTime Like "##:## [ap]m" Then
MsgBox "Time entered is not valid. Please enter time as hh:mm am/pm.", vbExclamation
Cancel = True
Exit Sub
End If
With Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 26)
.Value = TimeValue(txtArrivalTime)
.NumberFormat = "hh:mm" 'arrival time for checkbox calculation
End With
'''MEALS ALLOWED PER SPREADSHEET TO USERFORM'''
With Me.chkMorning
If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 28).Value = "T" Then
.Value = Checked
Else
.Value = Unchecked
.Enabled = False
End If
End With
With Me.chkMidday
If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 30).Value = "T" Then
.Value = Checked
Else
.Value = Unchecked
.Enabled = False
End If
End With
With Me.chkEvening
If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 32).Value = "T" Then
.Value = Checked
Else
.Value = Unchecked
.Enabled = False
End If
End With
'''END MOVEMENT OF MEALS ALLOWED TO USERFORM'''
End Sub
您的代码会禁用复选框,但不会重新启用它们。一旦禁用,它们的值将无法重置。我建议使用此代码,而不要使用您的代码。
Dim Check As Boolean
Check = (Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 28).Value = "T")
With chkMorning
.Enabled = vbTrue
.Value = Abs(Check)
.Enabled = .Value
End With
现在,所引用的工作表中的特定单元格通过哪种方法接收值“ T”,或者我不知道。我已经建议研究排序。鉴于对时间所做的更改将影响正在测试复选框的单元格,因此只有在VBA运行用户窗体时Excel重新计算工作表时,该更改才能生效。这我不知道。因此,我不会在此基础上建立。
基本方案是-或应该是-在打开(初始化)数据时将其读入表单,在用户查看表单时在表单中添加,修改或操纵这些数据,并交还其他经过修改或操纵的数据用户完成时(使用表单)将数据保存到工作表中。您的概念不遵循此方案。因此,它会产生您不必面对的问题。