我有2个表,表1和表2.两个表各有一个日期列。我将这个月的第一个星期一插入表1和表2之上。我将从表2的每一行获取日期值,如果它超过表顶部的值,我将插入0.如果表2中的日期值是" 16/02/2018& #34;,它不是星期一,我会在它之后插入星期一,并且该值为1。我怎么能继续它呢?请帮忙。
Dim col_tab2_dat as Date
first_day = DateSerial(Year(Date), Month(Date), 1)
last_day = DateSerial(Year(Date), Month(Date) + 1, 1)
curr_month= Format(first_day, "mmm")
w = Weekday(first_day , vbMonday)
FirstMonday = first_day + IIf(w <> 1, 8 - w, 0)
tab1_last_lin = ws.Columns(2).Find("Total(T1)").Row
tab2_last_lin = ws.Columns(2).Find("Total(T2)").Row
find_tab2 = ws.Columns(1).Find("Table 2").Row
last_lin = Range("B" & Rows.Count).End(xlUp).Row
last_col_tab1 = ws.Cells(tab1_last_lin, ws.Columns.Count).End(xlToLeft).Column
last_col_tab2 = ws.Cells(tab2_last_lin, ws.Columns.Count).End(xlToLeft).Column
last_dat = ws.Cells(2, last_col_tab1 - 1).Value
new_date = last_dat + 7
For i = find_tab2 + 3 to tab2_last_lin
ws.Cells(find_tab2 + 3, 1).Value = col_tab2_dat
If col_tab2_dat > last_dat Then
我被困在这里。下一步做什么?
End If
Next i
答案 0 :(得分:0)
这是一个将在下周一返回的函数:
Public Function GetNextMonday(dt As Date) As Date
Do Until Weekday(dt, vbSunday) = 2
dt = DateAdd("d", 1, dt)
Loop
GetNextMonday = dt
End Function
答案 1 :(得分:0)
这应该做你需要的:
If Weekday(Date) <= vbMonday Then
MsgBox Date + (vbMonday - Weekday(Date))
Else
MsgBox Date + 7 + (vbMonday - Weekday(Date))
End If
如果星期一已经过了本周,那么它会在 Else 语句中添加7天。 在Excel中,数字2表示星期一(取决于您的区域设置)。我使用常量2
编辑并替换vbMonday
。
您可以更进一步,并将其用于一周中的任何一天。创建一个像:
这样的函数Function getNextWeekday(dt As Date, wkDay As Long) As Date
If Weekday(dt) <= wkDay Then
getNextWeekday = dt + (wkDay - Weekday(dt))
Else
getNextWeekday = dt + 7 + (wkDay - Weekday(dt))
End If
End Function
然后你可以这样打电话:
'This will return next Monday's date
MsgBox = getNextWeekday(Date, vbMonday)
'This will return next Friday's date
MsgBox = getNextWeekday(Date, vbFriday)