简体   繁体   中英

VBA not changing all dates from text format to date format dd/mm/yyyy

I am having a problem for a long time now.. I have been trying to find some differences between dates and print the result in minutes. However, my problem is that even if I have added a line of code to convert the cell format to date format dd/mm/yyyy, it doesnt apply for the cells, but only for a part of them. Also, for some, it applies, but it creates the date format mm/dd/yyyy. I really dont what to do. These dates are vlookups from another sheet. So, I even wrote a line of code to transform them in their original sheets too. So , it total, just to be sure, I am making two convertions. My code is given below! please help me :)

Sheets("TMS").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
With Range("K2:K" & lastrow)
    If Not IsEmpty(Range("K2:K" & lastrow)) Then
        .NumberFormat = "mm/dd/yyyy hh:mm"
    End If
End With
Sheets("TheTracker").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
With Range("AO2:AO" & lastrow)
    .Formula = "=VLOOKUP(B2,TMS!B:K,10,FALSE)"
    .NumberFormat = "mm/dd/yyyy hh:mm"
    .Value = .Value
End With

Try to refer better to the Ranges, without using Select , refering to the worksheet that you need, and disregarding the hh:mm . Something like this may work probably:

Public Sub Test()

    Dim lastRow As Long

    With Worksheets("TMS")
        lastRow = .Range("B" & .rows.Count).End(xlUp).Row
        .Range("K2:K" & lastRow).NumberFormat = "mm/dd/yyyy"
    End With

    With Worksheets("TheTracker")
        lastRow = .Range("B" & .rows.Count).End(xlUp).Row
        With .Range("AO2:AO" & lastRow)
            .Formula = "=VLOOKUP(B2,TMS!B:K,10,FALSE)"
            .NumberFormat = "mm/dd/yyyy"
            .value = .value
        End With
    End With
End Sub

If a date format is applied to only a few of the cells, it looks like a disjoint of your computer's regional settings and the date format.

If you computer is set to show dates as DMY, then a date like

  • 1/1/2017 will be a real date - January 1
  • 12/7/2017 will be a real date - July 12
  • 13/7/2017 will be a real date - July 13

But

  • 7/13/2017 will not be a date, because the order of day and month are wrong.

The same goes for computers with regional settings to show dates as MDY, popular in the US.

  • 1/1/2017 will be a real date - January 1
  • 12/7/2017 will be a real date - December 7
  • 13/7/2017 will not be a real date - because there is no 13th month.

So, check what format the source data is displayed in: DMY or MDY?

Then adjust your VBA routine to pull the month and the day according to your computer's regional settings.

If you have the Dates as Text and Excel will not look at them as a date, you can try something like this.

Columns("K:K").Select
        Selection.TextToColumns Destination:=Range("K1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
            :=Array(1, 4), TrailingMinusNumbers:=True
            ActiveSheet.Range("K2").Select

I will guess that the data came from a CSV file that you OPEN 'd.

If that is the case, you are better off doing an IMPORT (usually on the DATA ribbon).

When you do this, the Text Import Wizard will open and you will be able to specify the date format of the incoming data.

Excel will then convert all of these dates to real dates, and you will be able to apply the number format. This IMPORT process can be automated through VBA if necessary.

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