Reputation: 636
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
Upvotes: 0
Views: 2429
Reputation: 60174
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.
Upvotes: 0
Reputation: 35900
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 112/7/2017
will be a real date - July 1213/7/2017
will be a real date - July 13But
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 112/7/2017
will be a real date - December 713/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.
Upvotes: 1
Reputation: 3230
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
Upvotes: 0
Reputation: 43565
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
Upvotes: 1