extract date but exclude Time

rikvny02

Board Regular
Joined
Aug 9, 2022
Messages
95
Office Version
  1. 365
Platform
  1. Windows
Hello All
The code below extracts the dates needed. However, there are instances when Time (1:00a) is entered into column C. How can I eliminate the conversion of time into a date? Please see last row results. Thanks for the help.

DatesResults
03-Dec-2023​
12/03/23​
02-Dec-2023​
12/02/23​
omit 12/3
12/03/23​
12/3 omit
12/03/23​
1:00a
01/00/00​

Sub DateExtract()
Dim rng As Range, R As Range
Dim I As Long
Dim S As String
Dim SA As Variant
Dim D As Date

With ActiveSheet
Set rng = .Range("c2", .Range("c" & .Rows.Count).End(xlUp))
rng.Offset(0, 1).NumberFormat = "mm/dd/yy"
End With

For Each R In rng
S = Application.Trim(R.Value)
SA = Split(S, " ")
For I = LBound(SA) To UBound(SA)
SA(I) = Replace(SA(I), ".", "-")
If VBA.IsDate(SA(I)) Then
D = CDate(SA(I))
If Len(Year(D)) < 4 Then
D = VBA.DateSerial(Year(Date), Month(D), Day(D))
End If
R.Offset(0, 1).Value = CStr(D)
End If
Next I
Next R
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If your data always has the format of 01:00a or 1:00p you can simply add an Iff statment like...
VBA Code:
If Right(S, 1) = "a" Or Right(S, 1) = "p" Then
    Else

end if
Here is the full example
VBA Code:
Sub DateExtract()
Dim rng As Range, R As Range
Dim I As Long
Dim S As String
Dim SA As Variant
Dim D As Date

With ActiveSheet
Set rng = .Range("c2", .Range("c" & .Rows.Count).End(xlUp))
rng.Offset(0, 1).NumberFormat = "mm/dd/yy"
End With

For Each R In rng
    S = Application.Trim(R.Value)
    If Right(S, 1) = "a" Or Right(S, 1) = "p" Then
    Else
        SA = Split(S, " ")
        For I = LBound(SA) To UBound(SA)
            SA(I) = Replace(SA(I), ".", "-")
            If VBA.IsDate(SA(I)) Then
                D = CDate(SA(I))
                If Len(Year(D)) < 4 Then
                    D = VBA.DateSerial(Year(Date), Month(D), Day(D))
                End If
                R.Offset(0, 1).Value = CStr(D)
            End If
        Next I
    End If
Next R
End Sub
 
Upvote 1
Solution
If your data always has the format of 01:00a or 1:00p you can simply add an Iff statment like...
VBA Code:
If Right(S, 1) = "a" Or Right(S, 1) = "p" Then
    Else

end if
Here is the full example
VBA Code:
Sub DateExtract()
Dim rng As Range, R As Range
Dim I As Long
Dim S As String
Dim SA As Variant
Dim D As Date

With ActiveSheet
Set rng = .Range("c2", .Range("c" & .Rows.Count).End(xlUp))
rng.Offset(0, 1).NumberFormat = "mm/dd/yy"
End With

For Each R In rng
    S = Application.Trim(R.Value)
    If Right(S, 1) = "a" Or Right(S, 1) = "p" Then
    Else
        SA = Split(S, " ")
        For I = LBound(SA) To UBound(SA)
            SA(I) = Replace(SA(I), ".", "-")
            If VBA.IsDate(SA(I)) Then
                D = CDate(SA(I))
                If Len(Year(D)) < 4 Then
                    D = VBA.DateSerial(Year(Date), Month(D), Day(D))
                End If
                R.Offset(0, 1).Value = CStr(D)
            End If
        Next I
    End If
Next R
End Sub
Thanks for the help
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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