先说阴历,后说节气,找节气请往下翻往下翻
代码测试好用,没啥问题。存储到模块里以后可以在单元格里直接 =nongli(A1),这样
代码来源网络。我也不知道谁写的就是了。
Public Function NongLi(Optional XX_DATE As Date)
Dim MonthAdd(11), NongliData(99), TianGan(9), DiZhi(11), ShuXiang(11), DayName(30), MonName(12)
Dim curTime, curYear, curMonth, curDay
Dim GongliStr, NongliStr, NongliDayStr
Dim i, m, n, k, isEnd, bit, TheDate
'获取当前系统时间
curTime = XX_DATE
'天干名称
TianGan(0) = "甲"
TianGan(1) = "乙"
TianGan(2) = "丙"
TianGan(3) = "丁"
TianGan(4) = "戊"
TianGan(5) = "己"
TianGan(6) = "庚"
TianGan(7) = "辛"
TianGan(8) = "壬"
TianGan(9) = "癸"
'地支名称
DiZhi(0) = "子"
DiZhi(1) = "丑"
DiZhi(2) = "寅"
DiZhi(3) = "卯"
DiZhi(4) = "辰"
DiZhi(5) = "巳"
DiZhi(6) = "午"
DiZhi(7) = "未"
DiZhi(8) = "申"
DiZhi(9) = "酉"
DiZhi(10) = "戌"
DiZhi(11) = "亥"
'属相名称
ShuXiang(0) = "鼠"
ShuXiang(1) = "牛"
ShuXiang(2) = "虎"
ShuXiang(3) = "兔"
ShuXiang(4) = "龙"
ShuXiang(5) = "蛇"
ShuXiang(6) = "马"
ShuXiang(7) = "羊"
ShuXiang(8) = "猴"
ShuXiang(9) = "鸡"
ShuXiang(10) = "狗"
ShuXiang(11) = "猪"
'农历日期名
DayName(0) = "*"
DayName(1) = "初一"
DayName(2) = "初二"
DayName(3) = "初三"
DayName(4) = "初四"
DayName(5) = "初五"
DayName(6) = "初六"
DayName(7) = "初七"
DayName(8) = "初八"
DayName(9) = "初九"
DayName(10) = "初十"
DayName(11) = "十一"
DayName(12) = "十二"
DayName(13) = "十三"
DayName(14) = "十四"
DayName(15) = "十五"
DayName(16) = "十六"
DayName(17) = "十七"
DayName(18) = "十八"
DayName(19) = "十九"
DayName(20) = "二十"
DayName(21) = "廿一"
DayName(22) = "廿二"
DayName(23) = "廿三"
DayName(24) = "廿四"
DayName(25) = "廿五"
DayName(26) = "廿六"
DayName(27) = "廿七"
DayName(28) = "廿八"
DayName(29) = "廿九"
DayName(30) = "三十"
'农历月份名
MonName(0) = "*"
MonName(1) = "正"
MonName(2) = "二"
MonName(3) = "三"
MonName(4) = "四"
MonName(5) = "五"
MonName(6) = "六"
MonName(7) = "七"
MonName(8) = "八"
MonName(9) = "九"
MonName(10) = "十"
MonName(11) = "十一"
MonName(12) = "腊"
'公历每月前面的天数
MonthAdd(0) = 0
MonthAdd(1) = 31
MonthAdd(2) = 59
MonthAdd(3) = 90
MonthAdd(4) = 120
MonthAdd(5) = 151
MonthAdd(6) = 181
MonthAdd(7) = 212
MonthAdd(8) = 243
MonthAdd(9) = 273
MonthAdd(10) = 304
MonthAdd(11) = 334
'农历数据
NongliData(0) = 2635
NongliData(1) = 333387
NongliData(2) = 1701
NongliData(3) = 1748
NongliData(4) = 267701
NongliData(5) = 694
NongliData(6) = 2391
NongliData(7) = 133423
NongliData(8) = 1175
NongliData(9) = 396438
NongliData(10) = 3402
NongliData(11) = 3749
NongliData(12) = 331177
NongliData(13) = 1453
NongliData(14) = 694
NongliData(15) = 201326
NongliData(16) = 2350
NongliData(17) = 465197
NongliData(18) = 3221
NongliData(19) = 3402
NongliData(20) = 400202
NongliData(21) = 2901
NongliData(22) = 1386
NongliData(23) = 267611
NongliData(24) = 605
NongliData(25) = 2349
NongliData(26) = 137515
NongliData(27) = 2709
NongliData(28) = 464533
NongliData(29) = 1738
NongliData(30) = 2901
NongliData(31) = 330421
NongliData(32) = 1242
NongliData(33) = 2651
NongliData(34) = 199255
NongliData(35) = 1323
NongliData(36) = 529706
NongliData(37) = 3733
NongliData(38) = 1706
NongliData(39) = 398762
NongliData(40) = 2741
NongliData(41) = 1206
NongliData(42) = 267438
NongliData(43) = 2647
NongliData(44) = 1318
NongliData(45) = 204070
NongliData(46) = 3477
NongliData(47) = 461653
NongliData(48) = 1386
NongliData(49) = 2413
NongliData(50) = 330077
NongliData(51) = 1197
NongliData(52) = 2637
NongliData(53) = 268877
NongliData(54) = 3365
NongliData(55) = 531109
NongliData(56) = 2900
NongliData(57) = 2922
NongliData(58) = 398042
NongliData(59) = 2395
NongliData(60) = 1179
NongliData(61) = 267415
NongliData(62) = 2635
NongliData(63) = 661067
NongliData(64) = 1701
NongliData(65) = 1748
NongliData(66) = 398772
NongliData(67) = 2742
NongliData(68) = 2391
NongliData(69) = 330031
NongliData(70) = 1175
NongliData(71) = 1611
NongliData(72) = 200010
NongliData(73) = 3749
NongliData(74) = 527717
NongliData(75) = 1452
NongliData(76) = 2742
NongliData(77) = 332397
NongliData(78) = 2350
NongliData(79) = 3222
NongliData(80) = 268949
NongliData(81) = 3402
NongliData(82) = 3493
NongliData(83) = 133973
NongliData(84) = 1386
NongliData(85) = 464219
NongliData(86) = 605
NongliData(87) = 2349
NongliData(88) = 334123
NongliData(89) = 2709
NongliData(90) = 2890
NongliData(91) = 267946
NongliData(92) = 2773
NongliData(93) = 592565
NongliData(94) = 1210
NongliData(95) = 2651
NongliData(96) = 395863
NongliData(97) = 1323
NongliData(98) = 2707
NongliData(99) = 265877
'生成当前公历年、月、日 ==> GongliStr
curYear = year(curTime)
curMonth = month(curTime)
curDay = day(curTime)
GongliStr = curYear & "年"
If (curMonth < 10) Then
GongliStr = GongliStr & "0" & curMonth & "月"
Else
GongliStr = GongliStr & curMonth & "月"
End If
If (curDay < 10) Then
GongliStr = GongliStr & "0" & curDay & "日"
Else
GongliStr = GongliStr & curDay & "日"
End If
'计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
TheDate = (curYear - 1921) * 365 + Int((curYear - 1921) / 4) + curDay + MonthAdd(curMonth - 1) - 38
If ((curYear Mod 4) = 0 And curMonth > 2) Then
TheDate = TheDate + 1
End If
'计算农历天干、地支、月、日
isEnd = 0
m = 0
Do
If (NongliData(m) < 4095) Then
k = 11
Else
k = 12
End If
n = k
Do
If (n < 0) Then
Exit Do
End If
'获取NongliData(m)的第n个二进制位的值
bit = NongliData(m)
For i = 1 To n Step 1
bit = Int(bit / 2)
Next
bit = bit Mod 2
If (TheDate <= 29 + bit) Then
isEnd = 1
Exit Do
End If
TheDate = TheDate - 29 - bit
n = n - 1
Loop
If (isEnd = 1) Then
Exit Do
End If
m = m + 1
Loop
curYear = 1921 + m
curMonth = k - n + 1
curDay = TheDate
If (k = 12) Then
If (curMonth = (Int(NongliData(m) / 65536) + 1)) Then
curMonth = 1 - curMonth
ElseIf (curMonth > (Int(NongliData(m) / 65536) + 1)) Then
curMonth = curMonth - 1
End If
End If
'生成农历天干、地支、属相 ==> NongliStr
NongliStr = "农历" & TianGan(((curYear - 4) Mod 60) Mod 10) & DiZhi(((curYear - 4) Mod 60) Mod 12) & "年"
NongliStr = NongliStr & "(" & ShuXiang(((curYear - 4) Mod 60) Mod 12) & ")"
'生成农历月、日 ==> NongliDayStr
If (curMonth < 1) Then
NongliDayStr = "闰" & MonName(-1 * curMonth)
Else
NongliDayStr = MonName(curMonth)
End If
NongliDayStr = NongliDayStr & "月"
NongliDayStr = NongliDayStr & DayName(curDay)
NongLi = NongliStr & NongliDayStr
End Function
再说节气,看代码貌似麻烦一点,需要天文学技巧?
单元格里直接 =getjq(A1,B1,1)就行 ,A1是年,B1是月
Function getjq(yy, mm, Optional gs As Integer = 0) '经校对并测试1900-2100几无误差-yzc51
jqmc = "小寒大寒立春雨水惊蛰春分清明谷雨立夏小满芒种夏至小暑大暑立秋处暑白露秋分寒露霜降立冬小雪大雪冬至"
v0 = 628.3319653318
t = 0 '第1步迭代
L0 = (48650621.66 + 6283319653.318 * t) / 10 ^ 7
W = (mm - 5 + (yy - 1999) * 24) * 15 * 3.1415926 / 180 'W指的是太阳黄经。1999年春分对应W=0,
'以后每W每增加15度对应下一个节气。
t = t + (W - L0) / v0 '第2步迭代
t2 = t * t
l1 = (48950621.66 + 6283319653.318 * t + 53 * t2 _
+ 334116 * Cos(4.67 + 628.307585 * t) + 2061 * Cos(2.678 + 628.3076 * t) * t) / 10 ^ 7
v1 = 628.332 + 21 * Sin(1.527 + 628.307585 * t)
t = t + (W - l1) / v1 '第3步迭代
t2 = t * t
t3 = t2 * t
t4 = t3 * t
L2 = (48950621.66 + 6283319653.318 * t + 52.9674 * t2 + 0.00432 * t3 - 0.001124 * t4 _
+ 334166 * Cos(4.669257 + 628.307585 * t) + 3489 * Cos(4.6261 + 1256.61517 * t) _
+ 350 * Cos(2.744 + 575.3385 * t) + 342 * Cos(2.829 + 0.3523 * t) _
+ 314 * Cos(3.628 + 7771.3771 * t) + 268 * Cos(4.418 + 786.0419 * t) _
+ 234 * Cos(6.135 + 393.021 * t) + 132 * Cos(0.742 + 1150.677 * t) _
+ 127 * Cos(2.037 + 52.9691 * t) + 120 * Cos(1.11 + 157.7344 * t) _
+ 99 * Cos(5.23 + 588.493 * t) + 90 * Cos(2.05 + 2.63 * t) _
+ 86 * Cos(3.51 + 39.815 * t) + 78 * Cos(1.18 + 522.369 * t) _
+ 75 * Cos(2.53 + 550.755 * t) + 51 * Cos(4.58 + 1884.923 * t) _
+ 49 * Cos(4.21 + 77.552 * t) + 36 * Cos(2.92 + 0.07 * t) _
+ 32 * Cos(5.85 + 1179.063 * t) + 28 * Cos(1.9 + 79.63 * t) _
+ 27 * Cos(0.31 + 1097.71 * t) + 2060.6 * Cos(2.67823 + 628.307585 * t) * t _
+ 43 * Cos(2.635 + 1256.6152 * t) * t + 8.72 * Cos(1.072 + 628.3076 * t) * t2 _
- 994 - 834 * Sin(2.1824 - 33.75705 * t) _
- 64 * Sin(3.5069 + 1256.66393 * t)) / 10 ^ 7
t = t + (W - L2) / v1 '第4步迭代
J2000 = 2451545
' JD = J2000 + t * 36525 - (64.7 + (yy - 2005) * 0.4) / 86400 + 8 / 24 '地球自转修正项 需完善
JD = J2000 + t * 36525 - deltaT(yy) / 86400 + 8 / 24 '地球自转修正项 已完善
Z = Int(JD + 0.5) '转换日期
F = JD + 0.5 - Z
a0 = Int((Z - 1867216.25) / 36524.25)
A = Z + 1 + a0 - Int(a0 / 4): If Z < 2299161 Then A = Z
B = A + 1524
C = Int((B - 122.1) / 365.25)
D = Int(365.25 * C)
E = Int((B - D) / 30.6001)
' d1 = B - d - Int(30.6001 * E) + F
If yy = 1923 Then tm = 1.5 / 4320 '此代码用于修正1923-02-20 的误差。已经校正1900-2100年的误差
d1 = B - D - Int(30.6001 * E) + F - tm
m1 = E - 13: If E < 14 Then m1 = E - 1
y1 = C - 4715: If m1 > 2 Then y1 = C - 4716
d2 = (d1 - Int(d1)) * 86400
hh1 = Int(d2 / 3600)
mm1 = Int(((d2 - hh1 * 3600) / 60))
mm2 = ((d2 - hh1 * 3600) / 60)
ss1 = Round((mm2 - mm1) * 60, 2)
getjq1 = y1 & Format(m1, "\-00\-") & Format(Int(d1), "00")
getjq = getjq1 & Format(hh1, " 00") & Format(mm1, "\:00") & Format(ss1, "\:00.00 ")
If gs = 1 Then getjq = getjq & Mid(jqmc, (mm Mod 24) * 2 + 1, 2)
If gs = 2 Then getjq = Mid(jqmc, (mm Mod 24) * 2 + 1, 2)
If gs = 3 Then getjq = getjq1
If gs = 4 Then getjq = DateSerial(y1, m1, Int(d1)) + d2 / 86400
' Debug.Print getjq
End Function
Public Function deltaT(year)
If year >= 2005 And year < 2014 Then
deltaT = 64.7 + (year - 2005) * 0.4
ElseIf year >= 2014 And year < 2114 Then
deltaT = -20 + 31 * ((year - 1820) / 100) ^ 2 _
+ (year - 2114) * ((-20 + 31 * ((2014 - 1820) / 100) ^ 2) _
- (64.7 + (2014 - 2005) * 0.4)) / 100
ElseIf year > 2114 Then
deltaT = -20 + 31 * ((year - 1820) / 100) ^ 2
ElseIf year >= -4000 And year < -500 Then
y1 = -4000: y2 = -500
t = (year - y1) / (y2 - y1) * 10
A = 108371.7: B = -13036.8: C = 392: D = 0
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= -500 And year < -150 Then
y1 = -500: y2 = -150
t = (year - y1) / (y2 - y1) * 10
A = 17201: B = -627.82: C = 16.17: D = -0.3413
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= -150 And year < 150 Then
y1 = -150: y2 = 150
t = (year - y1) / (y2 - y1) * 10
A = 12200.6: B = -346.41: C = 5.403: D = -0.1593
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 150 And year < 500 Then
y1 = 150: y2 = 500
t = (year - y1) / (y2 - y1) * 10
A = 9113.8: B = -328.13: C = -1.647: D = 0.0377
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 500 And year < 900 Then
y1 = 500: y2 = 900
t = (year - y1) / (y2 - y1) * 10
A = 5707.5: B = -391.41: C = 0.915: D = 0.3145
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 900 And year < 1300 Then
y1 = 900: y2 = 1300
t = (year - y1) / (y2 - y1) * 10
A = 2203.4: B = -283.45: C = 13.034: D = -0.1778
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1300 And year < 1600 Then
y1 = 1300: y2 = 1600
t = (year - y1) / (y2 - y1) * 10
A = 490.1: B = -57.35: C = 2.085: D = -0.0072
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1600 And year < 1700 Then
y1 = 1600: y2 = 1700
t = (year - y1) / (y2 - y1) * 10
A = 120: B = -9.81: C = -1.532: D = 0.1403
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1700 And year < 1800 Then
y1 = 1700: y2 = 1800
t = (year - y1) / (y2 - y1) * 10
A = 10.2: B = -0.91: C = 0.51: D = -0.037
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1800 And year < 1830 Then
y1 = 1800: y2 = 1830
t = (year - y1) / (y2 - y1) * 10
A = 13.4: B = -0.72: C = 0.202: D = -0.0193
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1830 And year < 1860 Then
y1 = 1830: y2 = 1860
t = (year - y1) / (y2 - y1) * 10
A = 7.8: B = -1.81: C = 0.416: D = -0.247
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1860 And year < 1880 Then
y1 = 1860: y2 = 1880
t = (year - y1) / (y2 - y1) * 10
A = 8.3: B = -0.13: C = -0.406: D = 0.0292
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1880 And year < 1900 Then
y1 = 1880: y2 = 1900
t = (year - y1) / (y2 - y1) * 10
A = -5.4: B = 0.32: C = -0.183: D = 0.0173
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1900 And year < 1920 Then
y1 = 1900: y2 = 1920
t = (year - y1) / (y2 - y1) * 10
A = -2.3: B = 2.06: C = 0.169: D = -0.0135
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1920 And year < 1940 Then
y1 = 1920: y2 = 1940
t = (year - y1) / (y2 - y1) * 10
A = 21.2: B = 1.69: C = -0.304: D = 0.0167
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1940 And year < 1960 Then
y1 = 1940: y2 = 1960
t = (year - y1) / (y2 - y1) * 10
A = 24.2: B = 1.22: C = -0.064: D = 0.0031
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1960 And year < 1980 Then
y1 = 1960: y2 = 1980
t = (year - y1) / (y2 - y1) * 10
A = 33.2: B = 0.51: C = 0.231: D = -0.0109
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 1980 And year < 2000 Then
y1 = 1980: y2 = 2000
t = (year - y1) / (y2 - y1) * 10
A = 51: B = 1.29: C = -0.026: D = 0.0032
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
ElseIf year >= 2000 And year < 2005 Then
y1 = 2000: y2 = 2005
t = (year - y1) / (y2 - y1) * 10
A = 63.87: B = 0.1: C = 0: D = 0
deltaT = A + B * t + C * t ^ 2 + D * t ^ 3
Else
deltaT = 108372
End If
End Function
- 相关评论
- 我要评论
-