格式化日期为特定格式的列 - Excel VBA

RXC*_*RXC 0 excel formatting vba excel-vba

我正在写一个excel vba宏.

我有一个超过10,000行的大型工作表.其中一列具有此格式的日期:10/28/13 06:57单元格具有用于格式化的常规编号.

我想将它格式化为只有这种格式的四位数字:( mmdd上例中为1028)

这是我到目前为止在子程序中的内容:

' This subroutine formats the columns to what is necessary for output
Sub formatColumns()
    ' Set the LastRow variable to hold the last row number
    lastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row

    Dim cell As Range

    For Each cell In Range("B3:B" & lastRow)
        cell.NumberFormat = "mmdd;@"
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

Dav*_*ens 5

我认为你发布的代码没有任何问题,所以我不太确定你要做什么,或者你当前的努力失败了,但你不需要For/Next循环来做到这一点,你可以将NumberFormat属性应用于整个范围:

Sub formatColumns()
    Dim lastRow as Long
    ' Set the LastRow variable to hold the last row number
    lastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row

    Range("B3:B" & lastRow).NumberFormat = "mmdd"

End Sub
Run Code Online (Sandbox Code Playgroud)