简体   繁体   English

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

[英]Format column with date to specific format - Excel VBA

I am writing an excel vba macro. 我正在编写一个Excel VBA宏。

I have a large worksheet with over 10,000 rows. 我有一个超过10,000行的大型工作表。 One of the columns has the date in this format: 10/28/13 06:57 with the Cells having a General Number for formatting. 列中的日期具有以下格式: 10/28/13 06:57 ,单元格具有用于格式化的通用编号。

I would like to format it to have only four digits in this format: mmdd (1028 for the example above) 我想将其格式化为只有四个数字: mmdd (上面的示例为1028)

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: 我认为您发布的代码没有任何问题,所以我不确定您要执行的操作或当前工作失败的地方,但是您不需要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

You can't really format it that way using that mmdd number format. 您无法真正使用mmdd数字格式来格式化它。 At least it doesn't work for me. 至少它对我不起作用。 (Excel 2010/ Win 7 European Locale) (Excel 2010 / Win 7 欧洲语言环境)

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. 同时,您将使用Month()Day()函数创建所需的格式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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