Anomalies using the split command

crunchie

New Member
Joined
Mar 10, 2011
Messages
8
Hi,

I am using the split command to plit a string in specific cells in my excel sheet. An example of the string is as follows;

Code:
<TABLE style="WIDTH: 86pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=115 border=0 x:str><COLGROUP><COL style="WIDTH: 86pt; mso-width-source: userset; mso-width-alt: 4205" width=115><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD class=xl24 style="BORDER-RIGHT: #f0f0f0; BORDER-TOP: #f0f0f0; BORDER-LEFT: #f0f0f0; WIDTH: 86pt; BORDER-BOTTOM: #f0f0f0; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent" width=115 height=17>[FONT=Arial][SIZE=2]2011-05-18[/SIZE][/FONT]</TD></TR></TBODY></TABLE>

I am recreating the date using the following code;

Code:
Sub compareDates()
 
' NOTE: When using Selection.Offset(a,b) a = vertical shift, b = horizontal shift
 
Range("H2").Select
 
Do Until Selection.Value = ""
 
 
strInput = Selection.Value
newDate = Split(strInput, "-")
 
Range("K2") = newDate(1)
Range("L2") = newDate(2)
 
 
If Range("K2").Value = Range("L2").Value Then
 
    Selection.Offset(0, 1).Value = "Error parsing date"
 
Else: Selection.Offset(0, 1).Value = newDate(1) & "/" & newDate(2) & "/" & newDate(0)
End If
 
Range("G11") = Date
 
If Selection.Offset(0, 1).Value > Range("G11").Value Then  
      Selection.Offset(0, 2).Value = "Service Not Due"
 
Else: Selection.Offset(0, 2).Value = "Service Due"
End If
 
Selection.Offset(1, 0).Select
 
Loop
 
End Sub

The new date is then in this format;

Code:
18/05/2011

The code works perfectly untill it runs into a date where the month number is the same as the day number i.e. 2nd Feb or 3rd March etc. I get the following o/p from the code;

Code:
05/05

Which is incorrect. It should be;

Code:
05/05/2011

Is there any way of preventing this error from happening? Ive tried to put in an if statement but it is not catching the error.

Any suggestions would be greatly appreciated.

Thanks in advance.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I'm not too sure so make a copy before you run this macro
Replace this code
Code:
If Range("K2").Value = Range("L2").Value Then
 
    Selection.Offset(0, 1).Value = "Error parsing date"
 
Else: Selection.Offset(0, 1).Value = newDate(1) & "/" & newDate(2) & "/" & newDate(0)
End If
with
Code:
Selection.Offset(0, 1).Value = newDate(1) & "/" & newDate(2) & "/" & newDate(0)
 
Upvote 0
To get the value of the cell as it appears, use the Text property rather than the Value property eg:

strInput = Selection.Text

rather than:

strInput = Selection.Value
 
Upvote 0
How about just

Code:
mydate = datevalue(range("H2").text)
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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