Link to home
Start Free TrialLog in
Avatar of upobDaPlaya
upobDaPlaya

asked on

Formatting date values in VBA for various iterations of a date

In Excel VBA I am trying to determine how I can consistently format the below value into dd/mm/yyyy if I receive the cell value provides the below values ?  I tried format.range(***,"dd/mm/yyyy") but the converted values  are inconsistent depending on how someone sends me the values (see below possible received values).

2/1/2017
2/1/17
2/01/17
02/01/2017
02/1/17
02/1/2017


All the above I need to to convert to 01/02/2017
ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of upobDaPlaya
upobDaPlaya

ASKER

An edit to the question - Once formatted though I need to store as text and can not drop any leading zeros.  Sorry for the omission.
Actually this helped...from


Sub SingleQuote()
Dim r As Range, s As String
 
For Each r In Selection
    If Not r.HasFormula Then
        s = r.Text                     ' gets text displayed in cell
        r.NumberFormat = "@"     ' sets cell format as text
        r.Value = s                    ' writes the new value in the cell
    End If
Next r
End Sub