简体   繁体   中英

Format column with date to specific format - Excel VBA

I am writing an excel vba macro.

I have a large worksheet with over 10,000 rows. One of the columns has the date in this format: 10/28/13 06:57 with the Cells having a General Number for formatting.

I would like to format it to have only four digits in this format: mmdd (1028 for the example above)

Here is what I have so far in the subroutine:

' 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

I don't think there's anything wrong with the code you posted, so I'm not quite sure what you're trying to do or where your current effort is failing, but you don't need a For/Next loop to do this, you can apply NumberFormat property to the entire range:

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

You can't really format it that way using that mmdd number format. At least it doesn't work for me. (Excel 2010/ Win 7 European Locale)

What I'd suggest it's adding an extra column and copying over the data to keep the original format then hiding that column in the end. Meanwhile, you would use Month() and Day() functions to create the desired format.

Sub formatColumns()
    Dim lastRow As Long

    ' 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;@"
        cell.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        cell.Offset(0, -1).NumberFormat = "@"
        cell.Offset(0, -1) = _
            IIf(Len(Month(cell)) = 1, "0" & Month(cell), Month(cell)) & _
            IIf(Len(Day(cell)) = 1, "0" & Day(cell), Day(cell))
    Next

    Columns("C:C").EntireColumn.Hidden = True
End Sub

if you don't want to add an extra column but you dont mind loosing the original format then you can just replace the contents of the cells using this code

Sub formatColumns()
    Dim lastRow As Long

    ' 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;@"
        cell = IIf(Len(Month(cell)) = 1, "0" & Month(cell), Month(cell)) & _
        IIf(Len(Day(cell)) = 1, "0" & Day(cell), Day(cell))
    Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM