IsDate and CDate Help

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
I import information into an excel database from a csv file, the date column contains various formats like this...

Data_Entry_Form_ver_25a.xlsm
ABC
1RINSurname, Given namesBirthDt
22644Dobell, Glenn3 Apr 1893
32645Weston, Vivienne Doris1896
43996Thornton,
53997Haydon, Hilton Bloomfield21-Jan-09
65607Humble, Alice Gloria21-May-11
75608Crowley, Annie
85626McIntyre, John Edwin1862
95627Young, Florence Caroline1862
105777Capel, Clive Ross13 Mar 1894
116981Hurley, Daniel Patrick10 Mar 1875
126982Hurley, Jerome John
137164Hammond, NevilleAbt. 1940
Sheet1


I am trying to create a loop and use the isdate and cdate function. This way at least all the full dates are in date format but I cannot get it right. Please help.
I only need to extract the year however I don't think I can do this in one step so I am only dealing with the fulldates at this stage.

This is my code
VBA Code:
Dim lRow_Dest As Long
Dim DateFormat As String
Dim i As Long
Dim DateSearch As Long

lRow_Dest = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(0).Row
DateFormat = IsDate.Value
DateSearch = Application.WorksheetFunction.CountIf(shImport.Range("C:C"), DateFormat)

    If DateSearch >= True Then
        For i = 2 To lDestLastRow
            If shImport.Cells(i, 3) = DateFormat Then
            Cells(i, 3).Value = CDate(Range(i, 3).Value)
            End If
        Next i
    End If
The error is argument not optional
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    58.9 KB · Views: 8

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Assuming column C is all text values, could you use a worksheet formula instead of vba?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

21 09 18.xlsm
CD
1BirthDtYear
23 Apr 18931893
318961896
4 
521-Jan-092009
621-May-112011
7 
818621862
918621862
1013 Mar 18941894
1110 Mar 18751875
12 
13Abt. 19401940
Year
Cell Formulas
RangeFormula
D2:D13D2=IF(C2="","",IFERROR(RIGHT(C2,4)+0,YEAR(DATEVALUE(C2))))
 
Upvote 0
Assuming column C is all text values, could you use a worksheet formula instead of vba?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

21 09 18.xlsm
CD
1BirthDtYear
23 Apr 18931893
318961896
4 
521-Jan-092009
621-May-112011
7 
818621862
918621862
1013 Mar 18941894
1110 Mar 18751875
12 
13Abt. 19401940
Year
Cell Formulas
RangeFormula
D2:D13D2=IF(C2="","",IFERROR(RIGHT(C2,4)+0,YEAR(DATEVALUE(C2))))
Hi Peter,
Thanks for your reply. This works perfectly on the data except for the dates eg. the date in row 5 is "21 Jan 1909" not "21 Jan 2009" and row 6 is "21 May 1911".

Also thanks for bringing my attention to me account details they are now updated. FYI Windows 10 & Office 365.
Dannielle
 
Upvote 0
Hi Peter,
Thanks for your reply. This works perfectly on the data except for the dates eg. the date in row 5 is "21 Jan 1909" not "21 Jan 2009" and row 6 is "21 May 1911".

Also thanks for bringing my attention to me account details they are now updated. FYI Windows 10 & Office 365.
Dannielle
I have
=IF(CELL("format",C2)="D1",(YEAR(C2)),(RIGHT(C2,4)))
this gives me the correct year for all formats. Thanks for your help I needed the nudge in the right direction.
Dannielle
 
Upvote 0
I have
=IF(CELL("format",C2)="D1",(YEAR(C2)),(RIGHT(C2,4)))
this gives me the correct year for all formats. Thanks for your help I needed the nudge in the right direction.
Dannielle

Are you sure ?
As far as I can tell it gives the same results as Peter's formula, except that it leaves some years a text and others as numbers.

If you know for sure that all the years are going to be < 2000, your formula needs minus 100 added to the Year version

Excel Formula:
=IFERROR(IF(CELL("format",C2)="D1",YEAR(C2)-100,RIGHT(C2,4)+0),"")
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

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