reformatting dates and finding latest (VBA)

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a worksheet with 5 dates in cells (A1, A101, A201, A301 & A401) - they are all formatted dd/mm/yyyy

I need to copy the date from these cells to other cells on another sheet but change the formats to yyyy-mm-dd - I thought I could use:

Code:
Date1 = Sheets("Sheet1").Range("A1").Value
Date1 = Format(Date1, "yyyy-mm-dd")
Sheets("Sheet2").Range("A1").Value = Date1

but that didn't work - it just keeps the same format...

Secondly, I need to work out the MAX or latest date in these 5 cells - I thought I could use:

Code:
EndDate1 = Sheets("Sheet1").Range("A1").Value
EndDate2 = Sheets("Sheet1").Range("A101").Value
EndDate3 = Sheets("Sheet1").Range("A201").Value
EndDate4 = Sheets("Sheet1").Range("A301").Value
EndDate5 = Sheets("Sheet1").Range("A401").Value
    
EndDateMax = WorksheetFunction.Max(EndDate1, EndDate2, EndDate3, EndDate4, EndDate5)

but it didn't like that either... obviously, doing stuff with dates isn't my strong point :(

If you can point me in the right direction, I'd be really grateful.

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Maybe this will help:
Code:
Sub test()
Date1 = Sheets("Sheet1").Range("A1").Value
With Sheets("Sheet2").Range("A1")
    .Value = Date1
    .NumberFormat = "yyyy-mm-dd"
End With
maxDate = Format(Application.Max(Range("A1,A101,A201,A301,A401")), "yyyy-mm-dd")
MsgBox maxDate
End Sub
 
Upvote 0
For the date format you just need to set the format of the cell rather than the data as the formatting of the cell will determine how it's displayed not the format of the data

Code:
Date1 = Sheets("Sheet1").Range("A1").ValueSheets("Sheet2").Range("A1").Value = Date1
Sheets("Sheet2").Range("A1").NumberFormat = "yyyy-mm-dd"


What problem are you havinng with your MAX function I can't see any reason it shouldn't be working, it should just return the highest number of days
 
Upvote 0
Thanks guys, I have applied your solutions. The first bit (changing the format) works great!

However, the 'finding the latest date' doesn't appear to work :(

It is displaying 1899-12-30, even though all 5 cells have a valid 'modern' date in them... why would this be?
 
Upvote 0
One possibility is that your "dates" are text not numbers. In any empty cell enter: =ISNUMBER(A1) where A1 contains one of your "dates". What does the formula return?
 
Upvote 0
Thanks Joe, that was it! the cell was formatted as text..... doh!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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