nnsea 发表于 2024-2-12 22:36:18

日期格式转化VBA


Sub ConvertDatesToStandardFormat()
    Dim LastRow As Long
    Dim i As Long
    Dim ws As Worksheet
      
    ' 设置工作表
    Set ws = ThisWorkbook.Sheets("Sheet1") ' 更改为您的工作表名称
      
    ' 查找A列中的最后一行
    With ws
      LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
      
    ' 遍历A列中的每个单元格并转换日期格式
    For i = 1 To LastRow
      If IsDate(ws.Cells(i, 1).Value) Then
            ' 转换日期格式
            ws.Cells(i, 1).Value = Format(ws.Cells(i, 1).Value, "yyyy-m-d")
            ' 可选:设置单元格的数字格式为文本,以防止Excel将其重新解释为日期
            ws.Cells(i, 1).NumberFormat = "@"
      End If
    Next i
      
    MsgBox "日期格式转换完成。"
End Sub
页: [1]
查看完整版本: 日期格式转化VBA