ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 958|回复: 8

[求助] 如何用正則求幾年幾月幾日的年月日及相加後年月日

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-4-17 06:20 | 显示全部楼层 |阅读模式
工作會用到幾年幾月幾日的相加及抽出年月日值

想要的結果在附件藍色字顯現,煩請各位大神先進們指導解惑
主要用正則,其他的方法也可提供


yy1.zip

2.32 KB, 下载次数: 9

TA的精华主题

TA的得分主题

发表于 2019-4-17 06:51 | 显示全部楼层
这么规范的数据,无须强求用正则,split就可以完美拆分年月日

相加可以使用日期格式相加,然后去提出数字,这样就不需要考虑进位问题

TA的精华主题

TA的得分主题

发表于 2019-4-17 11:47 | 显示全部楼层
樓主模擬的答案有問題, 詳看附件。

yy1.zip

2.32 KB, 下载次数: 7

TA的精华主题

TA的得分主题

发表于 2019-4-17 12:32 | 显示全部楼层
  1. Sub Test()
  2.     Dim rg As Range, arr As Variant, lngRow As Long
  3.     Dim strTemp As String, strSplit() As String
  4.     Dim intYear  As Integer, intMonth  As Integer, intDay As Integer
  5.    
  6.     Set rg = Sheet1.Range("F1:I5")
  7.     arr = rg
  8.    
  9.     For lngRow = 2 To 3
  10.         strTemp = arr(lngRow, 1)
  11.         strSplit = Split(strTemp, "年")
  12.         arr(lngRow, 2) = Val(strSplit(0))
  13.         
  14.         strTemp = strSplit(1)
  15.         strSplit = Split(strTemp, "月")
  16.         arr(lngRow, 3) = Val(strSplit(0))
  17.         
  18.         strTemp = strSplit(1)
  19.         strSplit = Split(strTemp, "日")
  20.         arr(lngRow, 4) = Val(strSplit(0))
  21.     Next
  22.    
  23.     intDay = (arr(2, 4) + arr(3, 4)) Mod 30
  24.     intMonth = (arr(2, 4) + arr(3, 4)) \ 30
  25.             
  26.     intMonth = intMonth + arr(2, 3) + arr(3, 3)
  27.     intYear = intMonth \ 12
  28.     intMonth = intMonth Mod 12
  29.    
  30.     intYear = intYear + arr(2, 2) + arr(3, 2)
  31.         
  32.     arr(5, 1) = intYear & "年" & intMonth & "月" & intDay & "日"
  33.    
  34.     rg = arr
  35. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-4-17 13:18 | 显示全部楼层
本帖最后由 爱吃蜂蜜的狼 于 2019-4-17 13:30 编辑

这个是拆分日期的(日期相加的你直接用if判断完用MOD函数取余数就可以了)

Sub test()
Set regx = CreateObject("vbscript.regexp")
'Dim regx As New RegExp
regx.Global = True
regx.Pattern = "\d+"
For Each Rng In Range("A2:A3,F2:F3")
Set mat = regx.Execute(Rng)
    Cells(Rng.Row, Rng.Column + 1) = mat(0).Value
    Cells(Rng.Row, Rng.Column + 2) = mat(1).Value
    Cells(Rng.Row, Rng.Column + 3) = mat(2).Value
Next
End Sub

TA的精华主题

TA的得分主题

发表于 2019-4-17 15:52 | 显示全部楼层
A-D列的
  1. Sub test()
  2.     Dim txt$, r%, i%, arr, brr(0 To 2) As Integer
  3.    
  4.     For r = 2 To 3
  5.         txt = Cells(r, "A").Value
  6.         For i = 1 To 3
  7.             txt = Replace(txt, Mid("年月日", i, 1), " ")
  8.         Next
  9.         arr = Split(Trim(txt))
  10.         With Cells(r, "B").Resize(1, 3)
  11.             .Value = arr
  12.             .Value = .Value
  13.         End With
  14.         For i = 0 To 2
  15.             brr(i) = brr(i) + arr(i)
  16.         Next
  17.     Next r
  18.    
  19.     If brr(2) > 30 Then '日进位
  20.         brr(1) = brr(1) + Int(brr(2) / 30)
  21.         brr(2) = brr(2) Mod 30
  22.     End If
  23.     If brr(1) > 12 Then '月进位
  24.         brr(0) = brr(0) + Int(brr(1) / 12)
  25.         brr(1) = brr(1) Mod 12
  26.     End If
  27.    
  28.     Cells(5, "A") = brr(0) & "年" & brr(1) & "月" & brr(2) & "日"
  29. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-4-17 21:08 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-4-17 21:11 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-10-19 14:07 | 显示全部楼层
shi353 发表于 2019-4-17 21:11
感謝,讓我完全了解可用split 多重取值的應用
完美解決了我的問題

您好,我有一个看到一个您之前做的一个实现word邮件合并的VBA,我参考你的那个VBA做了一个,但是无法实现循环,能帮忙看下问题出在哪里么,我等级太低,不能加您好友,我发了贴,能指导一下么?大神
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-27 06:49 , Processed in 0.040840 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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