excel - VBA 转换日期
问题描述
我究竟做错了什么 ?
在单元格 a2 中,我有一个日期(美国)格式 19790131。我想在 31.01.1979 中重写它enter code here
但得到结果:1/31/1979
For i = 2 To z
DatText = Cells(i, x)
Cells(i, x) = CDate(Mid(DatText, 7, 2) & "." & Mid(DatText, 5, 2) & "." & Mid(DatText, 1, 4))
Cells(i, x).NumberFormat = "dd.mm.yyyy"
Next i
解决方案
您可以为此使用Format - 并包含IsDate以进行一些错误处理:
Public Function ConvertDate()
Dim ThisCell As Excel.Range
Dim RowIndex As Long
Dim ColumnIndex As Long
Dim DatText As String
ColumnIndex = 1
For RowIndex = 2 To 10
Set ThisCell = Cells(RowIndex, ColumnIndex)
If Not IsDate(ThisCell.Value) Then
DatText = Format(ThisCell.Value, "@@@@\/@@\/@@")
If IsDate(DatText) Then
ThisCell.Value = CDate(DatText)
End If
End If
Next
End Function
推荐阅读
- logging - Slack 上的 Datadog 指标监控通知,带有指向日志浏览器的链接
- python-3.x - 试图找出两个日期之间的时间,然后让它们在 python 中可读
- css - 为什么我的反应原生项目中没有应用背景图像
- msgpack - c# MessagePack-CSharp 获取 MessagePackDynamicObjectResolverException:找不到匹配的构造函数。类型:System.String
- javascript - 监听窗口对象属性更改/创建
- javascript - 在输入值之前添加主题标签
- python - 更改一次或两次后,Discord 不会编辑 voice_channel 名称
- android - Google Play 无效签名 - 上传 APK
- triggers - 当用户选择“其他...”时,有没有办法从弹出菜单中触发脚本?
- css - 缓动动画究竟何时到达其中点?