based on your criteria, here is a simple function which is a bit rudimentary but it will work.
Code:
[table="width: 500"]
[tr]
[td]Function convertDate(ByVal strDate As String)
sYYYY = Right(strDate, 4)
Select Case Len(strDate)
Case 7
sM = "0" & Left(strDate, 1)
sD = Mid(strDate, 2, 2)
Case 8
sM = Left(strDate, 2)
sD = Mid(strDate, 3, 2)
End Select
convertDate = sM & "/" & sD & "/" & sYYYY ' (use this to have MM/DD/YYYY as a String type.
' convertDate = CDate(sM & "/" & sD & "/" & sYYYY) ' use this to convert to Date type (used for comparison)
End Function[/td]
[/tr]
[/table]
Just pointing out that your formula returns the converted date as a text string, not a real Excel date. If that is okay with the OP, then there is a one-liner equivalent for your function...
Code:
[table="width: 500"]
[tr]
[td]Function ConvertDate(ByVal strDate As String) As Variant
ConvertDate = Evaluate("IF(" & strDate & "=0,"""",TEXT(" & strDate & ",""0\/00\/0000""))")
End Function[/td]
[/tr]
[/table]
Note that I chose to return the empty text string ("") if the cell contained a 0 rather than the OP's date string. If the OP wants real dates, the same one-liner can be used by adding the 0+ that I show in red.
Code:
[table="width: 500"]
[tr]
[td]Function ConvertDate(ByVal strDate As String) As Variant
ConvertDate = Evaluate("IF(" & strDate & "=0,"""",[B][COLOR="#FF0000"]0+[/COLOR][/B]TEXT(" & strDate & ",""0\/00\/0000""))")
End Function[/td]
[/tr]
[/table]
Note that this version returns the real date as a date serial number meaning the OP will have to format the cells the UDF formula is placed in with the date format of his choosing.
Note to the OP: HOW TO INSTALL UDFs
---------------------------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use
ConvertDate just like it was a built-in Excel function. For example,
=ConvertDate (A2)
If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.