如果日期 = 今天更新行,否则创建新行
If date = today update row, else, create new row
我正在使用用户表单来记录一天中特定时间的计数。
我自动将日期和星期添加到前两个字段。与其他一天相比,一周中的一天有不同的字段,因此 if 语句:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
数据将在一天中的不同时间提交。
如何确定最后一行的日期值是否等于今天以更新行,或者如果日期不匹配则创建新行?
会多写一点,因为注释不适合代码。
一般来说,您应该适当地限定引用,因此在这种情况下使用您的用户表单,您需要指定 sheet/etc。
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
这将在您输入数据的命令按钮中,以确定数据的去向。如果您需要在初始化时从 sheet 中提取数据,则可以设置 textbox.value = .cell 引用...请注意,这两种情况不在同一模块中。
我正在使用用户表单来记录一天中特定时间的计数。
我自动将日期和星期添加到前两个字段。与其他一天相比,一周中的一天有不同的字段,因此 if 语句:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
数据将在一天中的不同时间提交。
如何确定最后一行的日期值是否等于今天以更新行,或者如果日期不匹配则创建新行?
会多写一点,因为注释不适合代码。
一般来说,您应该适当地限定引用,因此在这种情况下使用您的用户表单,您需要指定 sheet/etc。
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
这将在您输入数据的命令按钮中,以确定数据的去向。如果您需要在初始化时从 sheet 中提取数据,则可以设置 textbox.value = .cell 引用...请注意,这两种情况不在同一模块中。