Date comparison

southpawDiscGolf

New Member
Joined
Dec 2, 2008
Messages
13
How in VBA can you evaluate a cell in the "general" format and in the "date" format? I'd perfer do this entirely in VBA without use of a helper column in Excel.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Situation 1
Cell is formatted “General” and the user enter a date using the method mmddyy. Macro that will change this to mm/dd/yy
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
Situation 2
Cell is formatted “General” and the user enter a date using the method mm/dd/yy. Desired result is obtained
<o:p> </o:p>
Situation 3
Cell is formatted Date and the user enter a date using the method mm/dd/yy
Desired result is obtained
<o:p> </o:p>
Situation 4 – This is the problem.
Cell is formatted Date and the user enter a date using the method mmddyy (Excel takes this as the number of days from 1/1/1900 not the desired result). The VBA. listed below, needs to be modified to handle this


Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim DateStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Target.NumberFormat = "General"
Exit Sub
End If
If VarType(Target) = vbDate Then
Exit Sub
End If
Application.EnableEvents = False
'The following logic was found on Chip Pearson's website
With Target
If .HasFormula = False Then
Select Case Len(.Formula)
Case 4 ' e.g., 9298 = 2-Sep-1998
DateStr = Left(.Formula, 1) & "/" & _
Mid(.Formula, 2, 1) & "/" & Right(.Formula, 2)
Case 5 ' e.g., 11298 = 12-Jan-1998 NOT 2-Nov-1998
DateStr = Left(.Formula, 1) & "/" & _
Mid(.Formula, 2, 2) & "/" & Right(.Formula, 2)
Case 6 ' e.g., 090298 = 2-Sep-1998
DateStr = Left(.Formula, 2) & "/" & _
Mid(.Formula, 3, 2) & "/" & Right(.Formula, 2)
Case 7 ' e.g., 1231998 = 23-Jan-1998 NOT 3-Dec-1998
DateStr = Left(.Formula, 1) & "/" & _
Mid(.Formula, 2, 2) & "/" & Right(.Formula, 4)
Case 8 ' e.g., 09021998 = 2-Sep-1998
DateStr = Left(.Formula, 2) & "/" & _
Mid(.Formula, 3, 2) & "/" & Right(.Formula, 4)
Case Else
Err.Raise 0
End Select
.Formula = DateValue(DateStr)
End If
End With
'The preceding logic was found on Chip Pearson's website
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid date."
Application.EnableEvents = True
End Sub

<o:p> </o:p>
 
Upvote 0
I guess you could check that the value of the date is reasonable. For example 032309 would be 15 June 1988.
 
Upvote 0
Since this information will be birthday's back to 1908. The range of dates that could be reasonable is broad. Is there a way to know how the user entered a number either with or without "/"s that can be captured before Excel converts it?
 
Upvote 0
How about this:
Force the input to be text:
Look at the inputted text - if it has a "/" change the format to "Date" (and calculate the cell if necessary)
If is a number and doesn't have a slash - run it through the macro
If it isn't a number kick out an error message

Will this approach work?
 
Upvote 0
Do you know if it possible to force input to be text?

I hope that I've stated what I want to do. But in case there is any confusion. I want the user to have total flexibility to input date information in any format that they desire - either with or without slashes and have the system be smart enough to interpret the input and make the necessary changes.

I was starting with one approach, which obviously has bugs and isn't finished and I'm looking for help with an approach that will work. Is there an approach that will work?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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