Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert date format to general format?

I’m trying to convert the format of cells in column N from Date to General. Some cells get converted but others do not. tried with the Delimited option but it still doesn’t change all the cells. Eventually tried this loop to loop through each cell and try to change the cells’ formatting, but needs a tweak to get it to work. any support please to achieve that?

Dim Lrow As Long: Lrow = Sheets("test").Range("N" & Sheets("test").Rows.Count).End(xlUp).Row
Dim rg As Range: Set rg = Sheets("test").Range("N1:N" & Lrow)
Dim rCount As Long: rCount = rg.Rows.Count
Dim arr(): arr = rg.EntireRow.Columns(14).Value2
Dim v As Long

For v = 1 To rCount

arr(v, 1) = Format(Cells(v, 14).Value, "General") <------- this line needs a modification 

Next v
rg.EntireRow.Columns(14).Value = arr

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Looping through all cells takes lots of time. Below code set the format for column N (index is 14).

Sub demo()
    Dim Sht As Worksheet
    Set Sht = ActiveSheet
    Sht.Columns(14).NumberFormatLocal = "General"
    With Application.Intersect(Sht.UsedRange, Sht.Columns(14))
        .Formula = .Formula 
    End With
End Sub

If you don’t have to keep any format on column N, clear is the best method to reset the cell format.

Sub demo()
    Dim Sht As Worksheet, Rng As Range
    Dim arr
    Set Sht = ActiveSheet
    Set Rng = Application.Intersect(Sht.UsedRange, Sht.Columns(14))
    arr = Rng.Formula
    Sht.Columns(14).Clear
    Rng.Formula = arr
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading