|
楼主 |
发表于 2016-8-9 14:03
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub Sheet2_按钮1_Click()
-
- If Cells(1, 6) = "" Or IsNull(Cells(2, 6)) Or IsNull(Cells(3, 6)) Then
- MsgBox ("输入信息不全,请重新输入")
- Cells(1, 6).Select
- Exit Sub
- End If
-
- If Cells(1, 6) > Cells(2, 6) Then
- MsgBox ("输入有误,终止日早于起始日")
- Cells(1, 6).SetFocus
- Exit Sub
- End If
-
- Range("A:D").ClearContents
-
- Dim Startdate As Date, Enddate As Date, Mydate As Date, Monthlength As Integer, Daylength As Integer, Income As Double, Tempincome As Double, Tempincome2 As Double
- Startdate = Cells(1, 6).Value
- Enddate = Cells(2, 6).Value
- Income = Cells(3, 6).Value
- Monthlength = DateDiff("m", Startdate, Enddate)
- Daylength = DateDiff("d", Startdate, Enddate) + 1
- Tempincome = Income
-
- Cells(1, 1) = "年度"
- Cells(1, 2) = "月度"
- Cells(1, 3) = "天数"
- Cells(1, 4) = "金额"
- For i = 0 To Monthlength
- Mydate = DateAdd("m", i, Startdate)
- If Monthlength = 0 Then
- Cells(i + 2, 1) = Year(Mydate)
- Cells(i + 2, 2) = Month(Mydate)
- Cells(i + 2, 3) = Daylength
- Cells(i + 2, 4) = Income
- ElseIf i = 0 Then
- Tempincome2 = Round(Income / Daylength * (Day(DateSerial(Year(Mydate), Month(Mydate) + 1, 0)) - Day(Mydate) + 1), 2)
- Tempincome = Tempincome - Tempincome2
- Cells(i + 2, 1) = Year(Mydate)
- Cells(i + 2, 2) = Month(Mydate)
- Cells(i + 2, 3) = Day(DateSerial(Year(Mydate), Month(Mydate) + 1, 0)) - Day(Mydate) + 1
- Cells(i + 2, 4) = Tempincome2
-
- ElseIf i = Monthlength Then
- Tempincome2 = Round(Income / Daylength * (Day(DateSerial(Year(Mydate), Month(Mydate) + 1, 0)) - Day(Mydate) + 1), 2)
- Cells(i + 2, 1) = Year(Mydate)
- Cells(i + 2, 2) = Month(Mydate)
- Cells(i + 2, 3) = Day(Enddate)
- Cells(i + 2, 4) = Round(Tempincome, 2)
- Else
- Tempincome2 = Round(Income / Daylength * Day(DateSerial(Year(Mydate), Month(Mydate) + 1, 0)), 2)
- Tempincome = Tempincome - Tempincome2
- Cells(i + 2, 1) = Year(Mydate)
- Cells(i + 2, 2) = Month(Mydate)
- Cells(i + 2, 3) = Day(DateSerial(Year(Mydate), Month(Mydate) + 1, 0))
- Cells(i + 2, 4) = Tempincome2
- End If
- T = DateAdd("s", 0.1, Now)
- Do Until Now > T
- DoEvents
- Loop
- Next
- End Sub
复制代码 |
|