获取在excel中各种格式的日期单元格的年月部分 (VBA代码)
-
公共部分 --> 开始按钮
Private Sub CommandButton1_Click()
Err.Clear
On Error Resume Next
Dim aryALLData, colNames
Dim curRowNo, curColNo, iRowCount, iColCount
Const cnst_fldRowNo = 1
Const cnst_BeginDataRowNo = 2
'----------
If MsgBox("开始执行吗 ?", vbYesNo, "确认开始") <> vbYes Then Exit Sub
iColCount = UsedRange.Columns.Count
aryALLData = UsedRange
iRowCount = UBound(aryALLData, 1)
iColCount = UBound(aryALLData, 2)
'--- 获取 全部列名
Set colNames = CreateObject("scripting.dictionary")
For curColNo = 1 To iColCount
If aryALLData(cnst_fldRowNo, curColNo) <> "" Then
colNames.Add aryALLData(cnst_fldRowNo, curColNo), curColNo
Else
MsgBox "第" & curColNo & "列标题是空,无法继续"
Exit Sub
End If
Next curColNo
For curRowNo = cnst_BeginDataRowNo To iRowCount
Cells(curRowNo, colNames("开始1")).Value = GetFormatedValue3_YYYYMM(CStr(aryALLData(curRowNo, colNames("开始日期"))))
Cells(curRowNo, colNames("结束1")).Value = GetFormatedValue3_YYYYMM(CStr(aryALLData(curRowNo, colNames("结束日期"))))
Next curRowNo
End Sub
- 日期自动修正为 YYYY-MM格式的代码
-
Function GetFormatedValue3_YYYYMM(strInputDate As String) As String strInputDate = Trim(CStr(strInputDate)) If InStr(1, strInputDate, "在岗") + InStr(1, strInputDate, "在职") > 0 Then GetFormatedValue3_YYYYMM = strInputDate Exit Function ElseIf Not (IsNumeric(Left(strInputDate, 4))) Then GetFormatedValue3_YYYYMM = "有误:" & strInputDate Exit Function End If Dim intYear, intMonth As Integer Dim FirstFindedPos As Long If IsDate(Left(strInputDate, 4) & "-" & Mid(strInputDate, 5, 2)) Then intYear = Left(strInputDate, 4) intMonth = Mid(strInputDate, 5, 2) Else '--- 1. 删除无需的 strInputDate = Replace(strInputDate, "份", "") strInputDate = Replace(strInputDate, "日", "") '--- 2. 统一分隔符 - strInputDate = Replace(strInputDate, "-年", "-") strInputDate = Replace(strInputDate, "-月", "-") strInputDate = Replace(strInputDate, "年", "-") strInputDate = Replace(strInputDate, "月", "-") strInputDate = Replace(strInputDate, "/", "-") strInputDate = Replace(strInputDate, ".", "-") If InStr(1, strInputDate, "-") > 0 Then '--- 格式 ==-> 【-】 FirstFindedPos = InStr(1, strInputDate, "-") intYear = Left(strInputDate, InStr(1, strInputDate, "-") - 1) If InStr(FirstFindedPos + 1, strInputDate, "-") = 0 Then 'xxxx-mm intMonth = Mid(strInputDate, FirstFindedPos + 1, 2) Else intMonth = Mid(strInputDate, FirstFindedPos + 1, InStr(FirstFindedPos + 1, strInputDate, "-") - InStr(1, strInputDate, "-") - 1) End If End If End If If IsDate(intYear & "-" & intMonth) Then GetFormatedValue3_YYYYMM = "'" & Format(intYear & "-" & intMonth, "yyyy-mm") End Function
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2018-10-09 delphi 10 Seattle 第一个Android程序
2018-10-09 Delphi 中DataSnap技术网摘
2018-10-09 DELPHI XE Android 开发笔记
2018-10-09 10分钟10行代码开发APP(delphi 应用案例)
2018-10-09 Delphi 10 Seattle 小票打印控件TQ_Printer
2018-10-09 Delphi中ClientDataSet的用法小结
2018-10-09 delphixe10 android操作 打电话,摄像头,定位等