【发布时间】:2022-01-24 00:53:52
【问题描述】:
我似乎被困在看似非常简单的代码上。我对 VB 很陌生,所以如果我做了一些非常愚蠢的事情,请耐心等待。
这是一个时间表,我全年每天都记录我的开始和结束时间。
所以每年我都会制作一份 Excel 文件的新副本,并且我想覆盖上一年输入的时间,默认开始时间是早上 7 点,结束时间是下午 4:30 我可能可以直接在 Excel 中使用“Vlookup”来完成,但是这个时间表是一个需要 VBA 技能的项目
“开始新工作表”按钮会打开一个用户表单以供其他输入正常使用。
在 Private Sub TSSubmitButton_Click() 中,我定义了我需要的范围和字符串,并创建了一些 For Each Cell in Range Find Monday,然后将变量附加到星期一开始时间 (07:00:00) 和星期一结束时间 (16: 45:00)
(例如仅包括星期一和星期二)
调试不会标记任何错误,并且当代码运行时(即单击用户表单提交按钮)没有任何反应,所有时间值都保持原样。
我试过把“星期一作为字符串”变成“星期一作为日期”
我试过没有Find(DayCell.Value)
Private Sub TSSubmitButton_Click()
' Definitions
Dim DayRange As Range
Dim DayCell As Range
Set DayRange = Sheet1.Range("A2:A426")
Set DayCell = DayRange(1, 1)
Dim MonStartTime As String
Dim MonFinishTime As String
Dim TueStartTime As String
Dim TueFinishTime As String
Dim WedStartTime As String
Dim WedFinishTime As String
Dim ThuStartTime As String
Dim ThuFinishTime As String
Dim FriStartTime As String
Dim FriFinishTime As String
Dim SatStartTime As String
Dim SatFinishTime As String
Dim SunStartTime As String
Dim SunFinishTime As String
Dim Monday As String
Dim Tuesday As String
Dim Wednesday As String
Dim Thursday As String
Dim Friday As String
Dim Satday As String
Dim Sunday As String
MonStartTime = "07:00:00"
MonFinishTime = "16:45:00"
TueStartTime = "07:00:00"
TueFinishTime = "16:45:00"
WedStartTime = "07:00:00"
WedFinishTime = "16:45:00"
ThuStartTime = "07:00:00"
ThuFinishTime = "16:45:00"
FriStartTime = "00:00:00"
FriFinishTime = "00:00:00"
SatStartTime = "00:00:00"
SatFinishTime = "00:00:00"
SunStartTime = "00:00:00"
SunFinishTime = "00:00:00"
' loops the if statement through all cells and sets the time in adjecnt cells
For Each DayCell In DayRange.Find(DayCell.Value)
If DayCell = Monday Then
DayCell.Offset(, 2).Value = MonStartTime
DayCell.Offset(, 7).Value = MonFinishTime
End If
' loops the if statement through all cells and sets the time in adjecnt cells
For Each DayCell In DayRange.Find(DayCell.Value)
If DayCell = Tuesday Then
DayCell.Offset(, 2).Value = TueStartTime
DayCell.Offset(, 7).Value = TueFinishTime
End If
Next
Unload Me
End Sub
【问题讨论】: