我正在尝试使用DateAdd功能向日期添加一个月,日期格式为11/30/2018,但是无论输入什么日期,输出均为12:00:00 AM
oneCell和twoCell的范围均在Excel工作表中以日期格式设置
For Each aWorksheet In ActiveWorkbook.Worksheets
If Left(aWorksheet.Name, 1) = "#" Then
siteNum = Trim(Right(Left(aWorksheet.Name, 5), 4))
Set oneCell = aWorksheet.Range("C:C").Find("2018")
Set twoCell = aWorksheet.Range("D:D").Find("2018")
If oneCell.Row > twoCell.Row Then
Set oneCell = oneCell.Offset(-1, 0)
End If
Do While oneCell.Value <> ""
billMonthDate = GetBillMonth(oneCell.Value, twoCell.Value)
然后调用该函数:
Public Function GetBillMonth(ByVal startDate As Date, ByVal endDate As Date) As Date
Dim billMonth As Date
Dim startMonthDays As Integer
Dim endMonthDays As Integer
If DateDiff("m", startDate, endDate) > 1 Then
billMonth = DateTime.DateAdd("m", 1, startDate)
Else
startMonthDays = DateDiff("d", startDate, DateSerial(Year(startDate), Month(startDate) + 1, -1))
endMonthDays = Day(endDate)
If startMonthDays > endMonthDays Then
billMonth = oneCellDate
ElseIf startMonthDays < endMonthDays Then
billMonth = endDate
Else
'fuck me
End If
End If
GetBillMonth = billMonth
End Function
编辑: 开始日期来自我正在分析的电子表格。宏会循环遍历并查看许多日期,因此我不能只依次输入日期来解决此问题。我只需要dateadd即可工作
答案 0 :(得分:0)
始终使用实际日期值,而不使用看起来像日期的字符串。
Sub AddOneMonth()
Dim StartDate As Date
StartDate = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
Dim BillMonth As Date
BillMonth = DateAdd("m", 1, StartDate)
ThisWorkbook.Worksheets("Sheet1").Range("A2").Value = BillMonth
ThisWorkbook.Worksheets("Sheet1").Range("A2").NumberFormat = "MM/dd/yyyy"
End Sub
如果要处理单元格……
GRANT SELECT ON `Northwind`.`products` TO 'TestUser'@'%';
REVOKE ALL PRIVILEGES ON `Northwind`.`products` FROM 'TestUser'@'%';
答案 1 :(得分:0)
这是任何有兴趣的人的解决方法:
If Month(startDate) + 1 > 12 Then
billMonth = DateSerial(Year(startDate) + 1, 1, 1)
Else
billMonth = DateSerial(Year(startDate), Month(startDate) + 1, 1)
End If