首页 > 其他分享 >获取在excel中各种格式的日期单元格的年月部分 (VBA代码)

获取在excel中各种格式的日期单元格的年月部分 (VBA代码)

时间:2024-10-09 16:35:57浏览次数:1  
标签:VBA End strInputDate 单元格 excel Replace aryALLData InStr curRowNo

  1. 公共部分   --> 开始按钮

Private Sub CommandButton1_Click()
    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
            
            
    Dim retString As String
                    
    Err.Clear
    On Error Resume Next
    
    For curRowNo = cnst_BeginDataRowNo To iRowCount
        
        Cells(curRowNo, colNames("开始1")).Value = GetFormatedValue3_YYYYMM(aryALLData(curRowNo, colNames("开始日期")))
        Cells(curRowNo, colNames("结束1")).Value = GetFormatedValue3_YYYYMM(aryALLData(curRowNo, colNames("结束日期")))
        
    Next curRowNo
    
End Sub
  1.  日期自动修正为 YYYY-MM格式的代码
  2. Function GetFormatedValue3_YYYYMM(strInputDate As Variant) As String
        On Error Resume Next
    
        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

     

 

标签:VBA,End,strInputDate,单元格,excel,Replace,aryALLData,InStr,curRowNo
From: https://www.cnblogs.com/karkash/p/18454573

相关文章

  • EasyExcel读取合并单元格数据
    EasyExcelEasyExcel文档地址:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read一、前言当excel表格的数据表头和内容都比较工整,每个单元格对应一个数据时,通过EasyExcel可以很容易就将数据读取出来。但是当表格数据存在合并单元格时,还是按照EasyExc......
  • vue 前端导出 excel
    npminstall  xlsx-js-styleimportXLSXfrom'xlsx-js-style';//导出数据exportD(title,data,fileName){title=["标题1","标题2","标题3","标题4","标题5","标题6"];data=[["数据1&qu......
  • Excel:vba实现身份信息填写
     实现的效果是“点击一键填写性别和年龄”,表的呈现如下:代码如下:Sub判断性别年龄()DimidCardAsStringDimbirthDateAsStringDimnianAsInteger,yueAsInteger,riAsIntegerDimcurrentDateAsDateDimageAsIntegerDimgender......
  • 善用Excel,制作加班费统计表
    Hello,大家好。今天给大家分享由我制作的加班费统计表,该表由EXCEL完成,下面就和我一起来体验吧。填写节假日和加班时薪信息首先我们需要查询万年历,在下图的标记区域1中,将节假日及调休信息填写好。接着修改标记区域2,将基准填写为你所在城市的最低工资标准。平时指的是1.5倍......
  • table 单元格合并
    table元素合并单元格,用法倒是很简单,但过程中遇到了点小问题,记录下:1、多行多列合并,使用 rowSpan、colSpan设置要合并的行列数,再将合并后的多余单元格删除即可:functionmerge(table,px,py,row,col,remove=true){py--;lettarget=table.rows[px].......
  • POI导出excel文件加水印
    百分百能用,我用的POI版本是5.2.3,效果如下importlombok.extern.slf4j.Slf4j;importorg.apache.poi.openxml4j.opc.PackagePartName;importorg.apache.poi.openxml4j.opc.PackageRelationship;importorg.apache.poi.openxml4j.opc.TargetMode;importorg.apache.poi.xss......
  • 使用openpyxl读取Excel设置了公式的单元格 默认读取的是公式而不是公式结果
    使用openpyxl对单元格有公式的Excel的sheet进行操作,如果又读又写,可以采用双重读取方式解决。在Excel中,如果单元格的值是通过公式计算的,而你通过openpyxl库读取时发现获取的是公式而非结果,原因是openpyxl默认只读取单元格的公式而不计算公式的结果。为了获取公式计算后的......
  • Excel:vba实现根据输入的月份汇总数据的功能
    实现前的效果:实现后的效果:1.页面弹出想要汇总到的月份,就是指定月份的累计数2.输入月份之后开始进行汇总,汇总之后会弹出汇总成功的字样我输入1月份后,效果如下:代码如下:(可以删掉有关上面的b2单元格的几句话以及根据自己情况修改行和列,依然使用别的案例)Sub汇总数据()......
  • VBA日历进度
    hi,大家好!经过两次台风的洗礼之后,我们这里终于开始降温了,终于感觉到秋天的存在了!时间也在一天天的过去,马上要十一假期了,十一过了,就可以算着过年了,让今天就让我们来算算,离年底还剩余多少?(文章编辑时间为2024-09-23)那我们就来看看怎么做吧!01、创建窗体这里我们先来创建一个窗体,在窗体上......
  • winforms基本操作-将datagridview内容保存为excel文件
    这里记录一下将winforms展示的datagridview,导出或保存为excel文件。这里说一下环境、版本信息:win系统:win11框架:winforms依赖:Microsoft.Office.Interop.Excel.net:8.0.401.netframework:4.8DataGridView对象为dataGridView1,然后添加一个按钮,绑定事件btnConfirm即可。priva......