简体   繁体   中英

How to convert multiple column from text to date?

In my excel file, there are hundred columns. I want to convert some columns (eg. Column A, C,F, G..) from text to date. It really take time if manually change each column one column by one. How can I do it ? By using VBA ?

The original text column format just like '31-Dec-2011 00:00:00'. I want to convert to 'dd/mm/yyyy' 31/12/2011

Thanks

If you actually want to chop the time off in each of the cells then this might be an approach:

If the target times are in column A of a sheet called "target" and the column header in A1 is "StartTime"

You could amend the below and add an argument so that it takes the column address. Then it could be called several times to carry out this operation on several columns.

Sub ChopOffTime()

dim mySheetName as string
mysheetname = "target" '<<<<change to your sheet name

myLstRow = Excel.ActiveWorkbook.Sheets(mysheetname ).Cells(Excel.ActiveWorkbook.Sheets(mysheetname ).Rows.Count, Excel.ActiveWorkbook.Sheets(mysheetname).Range("A2").Column).End(Excel.xlUp).Row
With Excel.ActiveWorkbook.Sheets(mysheetname)

            .Columns("B:B").Insert Excel.xlToRight
            .Range("B2:B" & myLstRow).FormulaR1C1 = "=TIME(HOUR(RC[-1]),MINUTE(RC[-1]), SECOND(RC[-1]))"
            .Range("B1") = "StartTime" '<<<<<change to appropriate header
            .Columns("B:B").Copy
            .Columns("A:A").PasteSpecial Excel.xlPasteValues
            .Columns("B:B").Delete Excel.xlToLeft

End With

End Sub

Use the following for the columns in question:

Columns("A:A").Select
    Selection.NumberFormat = "m/d/yyyy"

Thanks all.

To simplified the procedure, I just developed some macro for user to run to change the column format, and then they can import the excel the mdb without error.

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