[英]How to convert date format values of a Column into only numbers in same Column in excel with VBA
对于下面的讨论,我假设这些值是“真实日期”,其格式将在您显示时显示。
此外,目标列的格式为 General 或 Number,没有小数位或分隔符。
如果不是这种情况,您可以手动添加。
你可以用一个简单的公式来做到这一点:
=--TEXT(H1,"ddmmyyy")
双一元将文本字符串转换为数字
如果您必须使用 VBA,请调整以下代码以符合您的实际源和目标要求:
Option Explicit
Sub dateToNumber()
Dim rSrc As Range
Dim vSrc As Variant
Dim I As Long
With ThisWorkbook.Worksheets("sheet13")
Set rSrc = Range(.Cells(1, "H"), .Cells(.Rows.Count, "H").End(xlUp))
End With
'Faster to work in arrays than back and forth to the worksheet
vSrc = rSrc
For I = 1 To UBound(vSrc, 1)
vSrc(I, 1) = Val(Format(vSrc(I, 1), "ddmmyyyy"))
Next I
'if you want to overwrite, remove Offset property from below line
With rSrc.Offset(columnoffset:=1)
.EntireColumn.Clear
.NumberFormat = "General"
.Value = vSrc
.EntireColumn.AutoFit
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.