使用新值自动替换excel中的整个列

时间:2014-09-13 16:25:58

标签: excel vba excel-vba

我有VBA代码,它将给定的特定日期格式转换为普通Date。有没有一种方法可以将整个列替换为我的新日期格式,可以通过单击按钮或仅使用函数。

我的代码是:

Function CONVDATE(myDate) As Date

    Dim arr

    arr = Split(myDate)

    CONVDATE = arr(1) & " " & arr(2) & " " & arr(5) & " " & arr(3)

End Function

2 个答案:

答案 0 :(得分:0)

立即在您的数据右侧输入:

=TEXT(LEFT(A1,11)&RIGHT(A1,4),"ddd mm dd yyyy")  

并双击填充手柄。然后在顶部选择,复制和粘贴特殊值。

答案 1 :(得分:0)

如果您需要VBA解决方案,并且对CONVDATE功能感到满意,可以使用以下内容转换整个列:

===================================

Option Explicit
Sub CONVALLDATES()
    Dim RNG As Range, C As Range

'Next line may change depending on how your range to convert is set up
Set RNG = Range("A1", Cells(Rows.Count, "A").End(xlUp))

'Uncomment next line when debugged.
'application.ScreenUpdating = False

For Each C In RNG
    With C
        .Value = CONVDATE(C)
        '.numberformat =  whatever format you want it displayed
    End With
Next C
Application.ScreenUpdating = True
End Sub

'---------------------------------------------
Function CONVDATE(myDate) As Date
    Dim arr
arr = Split(myDate)
CONVDATE = arr(1) & " " & arr(2) & " " & arr(5) & " " & arr(3)

End Function

====================================

您应该添加一些错误检查,以确保您尝试转换的字符串确实是特定格式。