month, year and date are coming wrong

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and Thanks in Advance![/]

I have a column with dates and Times llike eg:


12/07/2011 12:19
13/07/2011 7:40
30/08/2011 8:44

the formatting is dd/mm/yyyy h:mm;@

and I have to take the Year, Month and Day from them 'CAN'T use formulas on the Sheet
'Remember this code is just for this web

At present I am using the following code:

Code:
Sub MACRO1()
  On Error Resume Next:     Application.ScreenUpdating = False
Dim rPT As Range:           Set rPT = ActiveSheet.Range("A2:A500")
  Dim aPT():                aPT = rPT.Value
Dim xRow As Long

Dim vBreakDate As Variant
Dim vBrakDateTime As Variant


'rPT.Cells(xRow, 1) = "12/07/2011 12:19"
'rPT.Cells(xRow, 2) = "13/07/2011 7:40"
'rPT.Cells(xRow, 3) = "30/08/2011 8:44"



  For xRow = LBound(aPT, 1) To UBound(aPT, 1)
    vBrakDateTime = Split(rPT.Cells(xRow, 1), " ")
    ' Date = vBrakDateTime(0)
    ' Time = vBrakDateTime(1)
    vBreakDate = Split(vBrakDateTime(0), "/")
          ActiveSheet.Cells(xRow , 4) = vBreakDate(2) 'YEAR   ?????
          ActiveSheet.Cells(xRow , 5) = vBreakDate(1) 'MONTH  ?????   in my computer Works fine
          ActiveSheet.Cells(xRow , 6) = vBreakDate(0) 'DAY    ?????
  Next xRow

End Sub

In My Computer I get on the ActiveSheet:
yyyy mm dd
2011 7 12
2011 7 13
2011 8 30

so FINE.

But I my users computers they get:
yyyy mm dd
2011 12 7
2011 13 7
2011 30 8

so they get the Day instead of the month



And I don't know why
We are actually using the same workbook prepared for my to check what's wrong

DO I have to check country's code, language and/or???

What am I missing?


Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try using the date functions vs trying to break a date into a string:

Code:
Sub MACRO1()
  On Error Resume Next:     Application.ScreenUpdating = False
Dim rPT As Range:           Set rPT = ActiveSheet.Range("A2:A500")
  Dim aPT():                aPT = rPT.Value
Dim xRow As Long

Dim vBreakDate As Variant
Dim vBrakDateTime As Variant


'rPT.Cells(xRow, 1) = "12/07/2011 12:19"
'rPT.Cells(xRow, 2) = "13/07/2011 7:40"
'rPT.Cells(xRow, 3) = "30/08/2011 8:44"



  For xRow = LBound(aPT, 1) To UBound(aPT, 1)
    vBrakDateTime = Split(rPT.Cells(xRow, 1), " ")
    ' Date = vBrakDateTime(0)
    ' Time = vBrakDateTime(1)
    vBreakDate = Split(vBrakDateTime(0), "/")
         <strike> 'ActiveSheet.Cells(xRow, 4) = vBreakDate(2)  'YEAR   ?????</strike>
         <strike> 'ActiveSheet.Cells(xRow, 5) = vBreakDate(1)  'MONTH  ?????   in my computer Works fine</strike>
          <strike>'ActiveSheet.Cells(xRow, 6) = vBreakDate(0)  'DAY    ?????</strike>
          
          Cells(xRow, 4) = Year(rPT.Cells(xRow, 1))
          Cells(xRow, 5) = Month(rPT.Cells(xRow, 1))
          Cells(xRow, 6) = Day(rPT.Cells(xRow, 1))
  Next xRow

End Sub
 
Upvote 0
Try using the date functions vs trying to break a date into a string:

Cells(xRow, 4) = Year(rPT.Cells(xRow, 1))
Cells(xRow, 5) = Month(rPT.Cells(xRow, 1))
Cells(xRow, 6) = Day(rPT.Cells(xRow, 1))
Next xRow

End Sub
[/code]


Unfortunately when doing what you say, I did already try to do so.

the year goes up eg:

31/08/2011 19:33:14

it's giving me:
YEAR= 2013
Month=7
DAY=8
And even worst.

This is happenig just in some users computers, Because in my computer always goes FINE.
 
Upvote 0
Sounds like a good 'ole US/ROW date format problem.:)

That date is in dd/mm/yy, but for some reason VBA is interpreting it as being mm/dd/yy.

Try putting DateValue around rPT.Cells(xRow, 1) when you use the date functions that will give you a 'real' date.

Or put the 'real' date in a variable.
Code:
Dim dtPT As Date
 ...
 
dtPT = DateValue(rPT.Cells(xRow, 1))
 
Cells(xRow, 4) = Year(dtPT)
Cells(xRow, 5) = Month(dtPT)
Cells(xRow, 6) = Day(dtPT)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,722
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top