Need to compare 2 date variables. 1 variable is a string, the other variable is a date variable

adibakale

Board Regular
Joined
Apr 10, 2015
Messages
52
I need to compare 2 dates that are stored in variables. The problem I am having is that the first date is stored in a Date type variable, the 2nd is stored in a string type variable. The reason the 2nd date is in a string type variable is because it is pulling the date from a different application and is stored as a string in that application.

Here is the line of code that I am comparing.
dim t_date as date
dim cl_date as string

If t_date < cl_date then

Basically, I need to convert cl_date variable to a date type variable so that I can compare the 2 dates.

The format of t_date is 0/00/0000 and the format of cl_date is 00/00/0000.

any help with this is greatly appreciated
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
how about doing this comparison

Code:
If WorksheetFunction.Text(t_date, "mm/dd/yyy") = cl_date Then
 
Upvote 0
Regardless of how the cldate cell is fed, you should be able to get Excel to recognize a string of 00/00/0000 as a date if you dim it so. For example, if tdate is in A2, and cldate is in B2, you could do this:
Code:
Sub compdts()
Dim tdate As Date
Dim cldate As Date
tdate = Range("A2")
cldate = Range("B2")
If tdate < cldate Then
MsgBox ("tdate is earlier")
End If


End Sub
 
Upvote 0
You can still Dim cl_date As Date
Even if the source value is a String.

As long as that string is in a valid date format, then it will work.
 
Upvote 0

Forum statistics

Threads
1,215,401
Messages
6,124,705
Members
449,182
Latest member
mrlanc20

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