Excel VBA-数据清理 - 日期到字符串

时间:2017-03-08 22:13:37

标签: excel-vba vba excel

我需要清理数据表,最终为NCAA男子篮球队创建支架位置。不幸的是,我给出的一些数据已将记录(即10-20)转换为10月20日,而大多数其他记录仍为Win-Loss格式(即20-10)。任何人都可以就如何将日期格式(10月20日)转换回VBA工作表的预期赢利格式(10-20)提出任何建议吗?

任何帮助都会非常感激,因为我是新手,必须使用VBA来解决这个问题。

1 个答案:

答案 0 :(得分:2)

选择要转换的单元格并运行此短宏:

Sub fixdata()
    Dim v As Variant

    For Each r In Intersect(ActiveSheet.UsedRange, Selection)
        v = r.Value
        If v <> "" And IsDate(v) Then
            r.Value = "'" & Day(v) & "-" & Month(v)
        End If
    Next r
End Sub

在:

enter image description here

之后:

enter image description here

修改#1:

校正:

Sub fixdata()
    Dim v As Variant

    For Each r In Intersect(ActiveSheet.UsedRange, Selection)
        v = r.Value
        If v <> "" And IsDate(v) Then
            r.Value = "'" & Month(v) & "-" & Day(v)
        End If
    Next r
End Sub