|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 YZC51 于 2020-4-26 13:25 编辑
农历转公历函数(增强功能)
- Function NltoGl(nlrq, Optional gs As Integer) ' As Date
- Dim conDate As Date, setDate As Date, tDate As Date
- Dim valdate, IsGetGl As Boolean, bm$
- Dim AddMonth As Integer, AddDay As Integer, Addyear As Integer, getDay As Integer
- Dim i%, nMonth%, Myr%, r1, n1%
- Dim Sht2 As Worksheet
- nf = year(Date) + gs - 1
- yr = Mid(nlrq, 5)
- If gs Then nlrq = nf & yr
- Application.ScreenUpdating = False
- If Len(nlrq) = 0 Then NltoGl = "": Exit Function
- NYR = Split(nlrq, "-")
- tYear = NYR(0)
- tMonth = NYR(1)
- tDay = NYR(2)
- 'On Error Resume Next
- If tYear > 2200 Or tYear < 1900 Then Exit Function '不是有效日期则退出过程
- Call nonglibm
- bm = Nlbm(tYear)
- Addyear = tYear '农历正月初一的公历年份
- AddMonth = Val(Mid(bm, 15, 2)) '农历正月初一的公历月份
- AddDay = Val(Mid(bm, 17, 2)) '农历正月初一的公历日序
- conDate = DateSerial(Addyear, AddMonth, AddDay) '农历正月初一的公历日期
-
- AddDay = tDay
- If InStr(1, tMonth, "闰") > 0 Then
- nMonth = Val(Mid(tMonth, 2)) + 1
- Else
- nMonth = tMonth
- If Val("&H" & Mid(bm, 14, 1)) > 0 And tMonth > Val("&H" & Mid(bm, 14, 1)) Then nMonth = tMonth + 1
- End If
- For i = 1 To nMonth - 1
- AddDay = AddDay + 29 + Val(Mid(bm, i, 1))
- Next i
- setDate = DateAdd("d", AddDay - 1, conDate) - x
- NltoGl = Format(setDate, "yyyy-mm-dd")
- Application.ScreenUpdating = True
- End Function
复制代码 |
|