vba - how to use substitute for date conversion

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am using replace option to convert string date to Date format.
its working, how to use substitute function to achieve the same result.
attached are Column snapshot.



VBA Code:
Sub ConvertToDate()

Dim dt As Date
Dim rng As Range

Set rng = Range("H2", Range("H" & Rows.Count).End(xlUp))


With rng
    .Replace What:="/", Replacement:="/", LookAt:=xlPart
    .NumberFormat = "DD/MM/YYYY"
End With

'rng.TextToColumns Destination:=Range("h2")

dt = CDate(Application.Max(rng))

MsgBox dt

End Sub



Book2
H
1Transaction Date
209/04/20
309/04/20
409/06/20
501/02/18
601/02/18
701/02/18
801/03/18
901/03/18
Sheet1



Thanks
mg
 

Attachments

  • Snapdate Date Column.PNG
    Snapdate Date Column.PNG
    5.9 KB · Views: 3

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The Substitute function creates text so that will never work, the easiest way is with

=H2+0

as long as the dates are in the same format as your regional settings, converting mm/dd/yyyy dates to dd/mm/yyyy (for example) is better done with text to columns.

If you just want the max date then try =MAX(INDEX(H2:H9+0,0))
 
Upvote 0
Hi Jason,

Thanks for your help, this formula is giving correct result.
Can you help me how to store max date into vba variable. below formula
=MAX(INDEX(H2:H9+0,0))


Thanks
mg
 
Upvote 0
Hi Jason,


Thanks for your help your previous solution has worked, I was trying to find any other solution are there.
Just for learning purpose. thanks(y)


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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