Home » excel » Override reformatting of cell excel VBA

Override reformatting of cell excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a large number of excel sheets with a date column in the form “dd-mm-yyyy”. I need to sort the rows by the date in this date column. Manually if I select the column and replace all “-” with “/” then Excel auto formats the date cells from “General” to a “Date” number format so they can be easily sorted. Specifically it is the “*dd/mm/yyyy” since I am in Australia.

However when I recorded the macro it looks like this:

Range("D13:D134").Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

When I run the macro:

  • If the date has a day 12 or less then it flips the day and month fields and changes the number format to Date

  • Otherwise it leaves the number format as General.

Before: BEFORE
After: AFTER
I have tried setting the ReplaceFormat with:

Worksheets("Worksheet").Range("A1").NumberFormat = "d/mm/yyyy"
Application.ReplaceFormat.NumberFormat = "d/mm/yyyy"

Range("D13:D134").Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True

Now all the cells have a number format of date but still can’t be sorted as ascending (only A-Z option is available) and there is still the same issue with swapping date and month fields on certain cells.

I even tried to append an X to the start of the cell so it would remain a general format during the replace. Then removing the X after. Same result.

For x = 13 To 134
Cells(x, 4) = "X" & Cells(x, 4)
Next x

Range("D13:D134").Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

For x = 13 To 134
Range("D" + CStr(x)) = Right(Range("D" + CStr(x)), Len(Range("D" + CStr(x))) - 1)
Next x
How to&Answers:

Does it work for you?

On Error Resume Next
For x = 13 To 134
    If Range("D" & x) <> "" Then
        Range("D" & x) = CDate(Range("D" & x).Value)
    End If
Next x
On Error GoTo 0

Answer:

VBA isn’t required for this! You can fix the dates in 2 steps using excel functionality.

First you need to delimit the data:

  1. Highlight your dates
  2. Go to the ‘Data’ tab -> Click ‘Text to Columns’ in the Data Tools Section
  3. In the pop-up select ‘Delimited’, then click Next
  4. Under ‘Delimiters’ deselect ‘Tab’ and select ‘Other’. Type - into the box then click Next
  5. Pick a destination cell next to your data so it doesn’t get overwritten. Click Finish

Second, use the delimited data to create proper Excel dates
You can use the following formula to get a date from your delimited data:
=DATE( [cell with year], [cell with month], [cell with day])

Answer:

A Date Issue

Sub DateProblem()

    Const cStrSheet As String = "Sheet1"
    Const cStrRange As String = "D13:D134"
    Const cStrCell As String = "E13"

    Dim vntS As Variant
    Dim vntT As Variant
    Dim i As Long

    vntS = Worksheets(cStrSheet).Range(cStrRange)
    ReDim vntT(1 To UBound(vntS), 1 To 1) As Date

    For i = 1 To UBound(vntS)
        vntT(i, 1) = DateSerial( _
                Right(vntS(i, 1), 4), _
                Mid(vntS(i, 1), 4, 2), _
                Left(vntS(i, 1), 2))
    Next

    With Worksheets(cStrSheet).Range(cStrCell)
        .Resize(UBound(vntT), UBound(vntT, 2)) = vntT
    End With

End Sub