ROUNDUP当前date为15日或月底
我今天=今天(今天())得到一个“预期数组”的错误。 我不知道什么是错的。
Dim Thisday As Integer Dim Montho As Integer Dim Yearo As Integer Dim Lday As Integer Dim last as Integer last = Cells(Rows.Count, "A").End(xlUp).Row Thisday = Day(date) Montho = Month(date) Yearo = Year(date) Lday = Day(Application.WorksheetFunction.EoMonth(Date, -1))
然后在Excel中的B列填充相同的date,无论是15日还是最后一天。
If Thisday <= 15 Then Range("B2:B" & Last).Value = Montho & "/15/" & Yearo End If If Thisday > 15 Then Range("B2:B" & Last).Value = Montho & "/" & Lday & "/" & Yearo End If End Sub
也不是每个月都会在30号结束,那么Lday应该如何在每个月的某天返回。
使用Date
而不是Today
。
不要使用相同的名称作为function,excel会搞砸。
vba没有EoMonth
它是Application.WorksheetFunction的一部分。 它还返回一个date或双精度数,而不是一个整数。 你将需要得到的Day
:
Dim Today As Integer Dim Montho As Integer Dim Yearo As Integer Dim Lday As Integer Dim last As Integer last = Cells(Rows.Count, "A").End(xlUp).Row Today = Day(Date) Montho = Month(Date) Yearo = Year(Date) Lday = Day(Application.WorksheetFunction.EoMonth(Date, 0)) If Today <= 15 Then Range("B2:B" & Last).Value = Montho & "/15/" & Yearo End If If Today > 15 Then Range("B2:B" & Last).Value = Montho & "/" & Lday & "/" & Yearo End If
上面的代码返回一个看起来像date的string。 要返回一个真正的date使用这个:
Dim Today As Integer Dim Montho As Integer Dim Yearo As Integer Dim last As Integer last = Cells(Rows.Count, "A").End(xlUp).Row Today = Day(Date) Montho = Month(Date) Yearo = Year(Date) Range("B2:B" & Last).NumberFormat = "mm/dd/yyyy" If Today <= 15 Then Range("B2:B" & Last).Value = DateSerial(Yearo, Montho, 15) End If If Today > 15 Then Range("B2:B" & 4).Value = Application.WorksheetFunction.EoMonth(Date, 0) End If
尝试,
Range("B2:B" & Last).Value = dateserial(year(date), month(date)-(day(date)>15), 15 * abs(day(date)<=15))