ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 时光鸟

[分享] 农历与公历互相转换的自定义函数(强大)

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2013-11-13 20:56 | 显示全部楼层
本帖已被收录到知识树中,索引项:自定义函数开发
农历转公历显示格式为2014年1月1日怎么改?现在是2014/1/1

TA的精华主题

TA的得分主题

发表于 2016-3-3 11:16 | 显示全部楼层
农历转公历,输入:tYear=2014,tMonth=1,tDay=1
程序运行到134句就直接跳到End Function,是怎么回事?

TA的精华主题

TA的得分主题

发表于 2016-3-3 16:20 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2017-3-21 21:44 | 显示全部楼层
太强了,不董,但能用了,谢谢!

TA的精华主题

TA的得分主题

发表于 2017-10-9 10:32 | 显示全部楼层
请教:

农历常量(1899~2100,共202年)
Private Const ylData = "AB500D2,4BD0883," _   .......

其中AB500D2等,什么意思?怎么来的?

TA的精华主题

TA的得分主题

发表于 2017-10-27 10:52 | 显示全部楼层
校正后的公历农历互转函数
'阴阳历转换 和 阴阳历生日
'Version: 1.1 2005-9-1
'Author: James Zhuang
'Lunar(SolarDate[, Part = 0 | 1 | 2 | 3])   阳历转换成阴历
'                         Part = 0, all; Part = 1, lunar year; Part = 2, lunar month; Part = 3, lunar day
'Solar(LunarDate[, LunarMonth = 0 | 1])     阴历转换成阳历
Type ConvDataA
    leapmonth As Integer
    month(1 To 13) As Integer
    sp_month As Integer 'Solar month of Spring Festival
    sp_day As Integer   'Solar day   of Spring Festival
End Type
Private Function LunarData(q_year) As ConvDataA
    Dim d As Long
    Dim month(1 To 13) As Integer
'1901-2100
LunarCal = Array( _
&H4AE53, &HA5748, &H5526BD, &HD2650, &HD9544, &H46AAB9, &H56A4D, &H9AD42, &H24AEB6, &H4AE4A, _
&H6A4DBE, &HA4D52, &HD2546, &H5D52BA, &HB544E, &HD6A43, &H296D37, &H95B4B, &H749BC1, &H49754, _
&HA4B48, &H5B25BC, &H6A550, &H6D445, &H4ADAB8, &H2B64D, &H95742, &H2497B7, &H4974A, &H664B3E, _
&HD4A51, &HEA546, &H56D4BA, &H5AD4E, &H2B644, &H393738, &H92E4B, &H7C96BF, &HC9553, &HD4A48, _
&H6DA53B, &HB554F, &H56A45, &H4AADB9, &H25D4D, &H92D42, &H2C95B6, &HA954A, &H7B4ABD, &H6CA51, _
&HB5546, &H555ABB, &H4DA4E, &HA5B43, &H352BB8, &H52B4C, &H8A953F, &HE9552, &H6AA48, &H7AD53C, _
&HAB54F, &H4B645, &H4A5739, &HA574D, &H52642, &H3E9335, &HD9549, &H75AABE, &H56A51, &H96D46, _
&H54AEBB, &H4AD4F, &HA4D43, &H4D26B7, &HD254B, &H8D52BF, &HB5452, &HB6A47, &H696D3C, &H95B50, _
&H49B45, &H4A4BB9, &HA4B4D, &HAB25C2, &H6A554, &H6D449, &H6ADA3D, &HAB651, &H95746, &H5497BB, _
&H4974F, &H64B44, &H36A537, &HEA54A, &H86B2BF, &H5AC53, &HAB647, &H5936BC, &H92E50, &HC9645, _
&H4D4AB8, &HD4A4C, &HDA541, &H25AAB6, &H56A49, &H7AADBD, &H25D52, &H92D47, &H5C95BA, &HA954E, _
&HB4A43, &H4B5537, &HAD54A, &H955ABF, &H4BA53, &HA5B48, &H652BBC, &H52B50, &HA9345, &H474AB9, _
&H6AA4C, &HAD541, &H24DAB6, &H4B64A, &H6A573D, &HA4E51, &HD2646, &H5E933A, &HD534D, &H5AA43, _
&H36B537, &H96D4B, &HB4AEBF, &H4AD53, &HA4D48, &H6D25BC, &HD254F, &HD5244, &H5DAA38, &HB5A4C, _
&H56D41, &H24ADB6, &H49B4A, &H7A4BBE, &HA4B51, &HAA546, &H5B52BA, &H6D24E, &HADA42, &H355B37, _
&H9374B, &H8497C1, &H49753, &H64B48, &H66A53C, &HEA54F, &H6AA44, &H4AB638, &HAAE4C, &H92E42, _
&H3C9735, &HC9649, &H7D4ABD, &HD4A51, &HDA545, &H55AABA, &H56A4E, &HA6D43, &H452EB7, &H52D4B, _
&H8A95BF, &HA9553, &HB4A47, &H6B553B, &HAD54F, &H55A45, &H4A5D38, &HA5B4C, &H52B42, &H3A93B6, _
&H69349, &H7729BD, &H6AA51, &HAD546, &H54DABA, &H4B64E, &HA5743, &H452738, &HD164A, &H8E933E, _
&HD5252, &HDAA47, &H66B53B, &H56D4F, &H4AE45, &H4A4EB9, &HA2D4C, &HD1541, &H2D92B5, &HD5349)
startyear = 1901
ng = LunarCal(q_year - startyear)
    d = &H100000
    LunarData.leapmonth = Int(ng / d)
    ng = ng Mod d
   
    d = &H80
    mdata = Int(ng / d)
    ng = ng Mod d
   
    d = &H20
    LunarData.sp_month = Int(ng / d)
    LunarData.sp_day = ng Mod d
   
    d = &H1000
    i = 1
    Do
        LunarData.month(i) = 29 + Int(mdata / d)
        mdata = mdata Mod d
        If d = 1 Then Exit Do
        d = d / 2
        i = i + 1
    Loop
    If LunarData.leapmonth = 0 Then
       LunarData.month(13) = 0
    Else
       b = LunarData.month(LunarData.leapmonth + 1)
       For i = LunarData.leapmonth + 1 To 12
           LunarData.month(i) = LunarData.month(i + 1)
       Next i
       LunarData.month(13) = b
    End If
End Function
Function lunar(Solar_date As Date, Optional Part As Integer = 0) As String
'Part = 0, all; Part = 1, lunar year; Part = 2, lunar month; Part = 3, lunar day
Dim a As ConvDataA
l_year = Year(Solar_date)
a = LunarData(l_year)
sp_date = DateSerial(l_year, a.sp_month, a.sp_day)
If sp_date > Solar_date Then
    l_year = l_year - 1
    a = LunarData(l_year)
    sp_date = DateSerial(l_year, a.sp_month, a.sp_day)
End If

l_day = Solar_date - sp_date
l_month = 1
IS_lunar_leapmonth = False

y = a.month(l_month)
Do While l_day >= y
    l_day = l_day - y
    If l_month = a.leapmonth Then IS_lunar_leapmonth = (Not IS_lunar_leapmonth)
    If IS_lunar_leapmonth Then
        y = a.month(13)  '???
    Else
        l_month = l_month + 1
        y = a.month(l_month)
    End If
Loop

l_day = l_day + 1
lunar = l_year & "-" & l_month & "-" & l_day
If IS_lunar_leapmonth Then lunar = lunar & "-L"
lunar = Choose(Part + 1, lunar, l_year, l_month, l_day)
End Function
Function solar(Lunar_date, Optional IS_lunar_leapmonth As Integer = 0) As String
'IS_lunar_leapmonth = 0, No leap month; IS_lunar_leapmonth = 1, is leap month
Dim a As ConvDataA
Lunar_date = Split(Lunar_date, "-")
s_year = Lunar_date(0)
For Each C In Lunar_date
    If C = "L" Then IS_lunar_leapmonth = 1
Next
a = LunarData(s_year)
sp_date = DateSerial(s_year, a.sp_month, a.sp_day)
If Lunar_date(1) <> a.leapmonth Then IS_lunar_leapmonth = 0
x = Lunar_date(2)
tm = Lunar_date(1) + IS_lunar_leapmonth - 1
For i = 1 To tm
    x = x + a.month(i)
    If i = a.leapmonth And IS_lunar_leapmonth = 0 Then
        x = x + a.month(13)
    End If
Next
s_date = sp_date + x - 1
solar = s_date
End Function
Function lunarbirth(Solar_birthday As Date, Optional Inquire_year As Integer) As String
    If Inquire_year = 0 Then
        Inquire_year = Left(lunar(Now), 4)
        lunarbirth = solar(Inquire_year & Mid(lunar(Solar_birthday), 5, 10))
        If CDate(lunarbirth) < Now - 1 Then Inquire_year = Inquire_year + 1
    End If
    lunarbirth = solar(Inquire_year & Mid(lunar(Solar_birthday), 5, 10))
End Function
Function solarbirth(Solar_birthday As Date, Optional Inquire_year As Integer) As String
    If Inquire_year = 0 Then
        Inquire_year = Year(Now)
        solarbirth = DateSerial(Inquire_year, month(Solar_birthday), Day(Solar_birthday))
        If CDate(solarbirth) < Now - 1 Then Inquire_year = Inquire_year + 1
    End If
    solarbirth = DateSerial(Inquire_year, month(Solar_birthday), Day(Solar_birthday))
End Function
'校正:YZC51

补充内容 (2018-11-9 22:49):
重新矫正的数据,在31楼!

TA的精华主题

TA的得分主题

发表于 2018-1-2 10:10 | 显示全部楼层
正需要这个函数,下载收藏了,谢谢分享!

TA的精华主题

TA的得分主题

发表于 2018-1-2 16:34 | 显示全部楼层
ugyun 发表于 2013-6-16 13:07
经测试,里面的自定义函数GetYLDate有问题,如农历日期1955年十月初一,对应的公历日期应为1955-11-14, ...


关于2013年五月初一的公历不对的问题,在当年也是有被他人发现的,并做了纠正。对于1955年十月初一的公历对应问题,也可以查出相应的值去纠正的。
下面截图说明了这一点。

捕获.PNG

TA的精华主题

TA的得分主题

发表于 2018-3-4 21:14 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-3-5 10:02 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
9楼提出的问题,除了18楼修正2013-6-8外,1955-11-14是改457037C为45F037C,就可以正确计算了,原理上面的注释已经写清楚了,就是前三位16进制的代码,转换成二进制1是30天,0是29天,1955年11月14这天判断成了十月初二,说明本来九月应该是30天,他算成了29天,所以把原来457的二进制010001010111,改成010001011111,还原到16进制就是45F,这样一改就可以算正确了,其实根据这个思路写下去,可以写很多年的,不仅限于这202年,不知道我说得对不对
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-15 22:37 , Processed in 0.045389 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表