本帖最后由 松叶落 于 2021-2-8 15:26 编辑
天干地支,阴阳五行?有点意思,比以前的3D排列五什么的感觉高大上哈哈哈。单独的天干或地支倒还好说,天干地支组合起来的的六十甲子纳音五行好像没看明白是什么数学计算关系
干脆不想算法了直接弄个金木水火土的对照表挨个查找得了哈哈另外阴阳没打算考虑进去吗,比如木还有阳木和阴木
如果不用数组方式输入的话(单元格数组运行效率不高),直接使用str2num("甲子") 得到4或者num2str(str2num("甲子")) 得到金即可
Function wuxing(wx, Optional cs = 0)
'批量转换单个或多个天干地支字符到五行
a = wx
If cs = 0 Then
If IsArray(a) Then
ReDim b(1 To UBound(a), 1 To 1)
For i = 1 To UBound(a)
b(i, 1) = num2str(str2num(a(i, 1)))
Next
wuxing = b
Else
wuxing = num2str(str2num(a))
End If
Else
If IsArray(a) Then
ReDim b(1 To UBound(a), 1 To 1)
For i = 1 To UBound(a)
b(i, 1) = str2num(a(i, 1))
Next
wuxing = b
Else
wuxing = str2num(a)
End If
End If
End Function
Function str2num(str)
a = str
'天干、地支的字符转换为五行数字,用查对照表的方法
jin = Array("甲子", "乙丑", "壬申", "癸酉", "庚辰", "辛巳", "甲午", "乙未", "壬寅", "癸卯", "庚戌", "辛亥", "庚", "辛", "申", "酉")
mu = Array("戊辰", "己巳", "壬午", "癸未", "庚寅", "辛卯", "戊戌", "己亥", "壬子", "癸丑", "庚申", "辛酉", "甲", "乙", "寅", "卯")
shui = Array("丙子", "丁丑", "甲申", "乙酉", "壬辰", "癸巳", "丙午", "丁未", "甲寅", "乙卯", "壬戌", "癸亥", "壬", "癸", "子", "亥")
huo = Array("丙寅", "丁卯", "甲戌", "乙亥", "戊子", "己丑", "丙申", "丁酉", "甲辰", "乙巳", "戊午", "己未", "丙", "丁", "巳", "午")
tu = Array("庚午", "辛未", "戊寅", "己卯", "丙戌", "丁亥", "庚子", "辛丑", "戊申", "己酉", "丙辰", "丁巳", "戊", "己", "丑", "辰", "未", "戌")
'字符长度只能是1或2,否则就是错误输入
la = Len(a)
str2num = ""
If la = 1 Or la = 2 Then
wxarr = Array(tu, shui, huo, mu, jin)
For i = 0 To UBound(wxarr)
If inarr(a, wxarr(i)) Then
str2num = i
Exit For
End If
Next
End If
End Function
Function num2str(num)
a = num
'五行数字转为字符
wx = "土水火木金"
If a = "" Then
num2str = ""
Else
num2str = Mid(wx, a + 1, 1)
End If
End Function
Function inarr(str, strarr) As Boolean
'判断指定的字符是否属于某组字符
a = str
For Each s In strarr
If s = a Then
inarr = True
Exit For
End If
Next
End Function
|