我在Today = Day(Today())上遇到“预期数组”错误。我不知道出了什么问题。
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列将填入相同的日期,即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返回当月的日期。
答案 0 :(得分:3)
使用Date
代替Today
。
不要使用与函数相同的名称,excel会搞砸。
vba没有EoMonth
它是Application.WorksheetFunction的一部分。它还返回一个日期或双精度而不是整数。您需要获得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
上面的代码返回一个看起来像日期的字符串。要返回真实日期,请使用:
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
答案 1 :(得分:1)
尝试,
Range("B2:B" & Last).Value = dateserial(year(date), month(date)-(day(date)>15), 15 * abs(day(date)<=15))