View Single Post
  #14   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default parse cell contents

Here is an example where the data is split into adjacent columns only if it is
valid. If invalid, a message is placed in the adjacent column:

======================================
Option Explicit

Sub Validate()
Dim c As Range
Dim strDate As String
Dim strF As String
Dim i As Long
Dim ValidEntry As Boolean
Dim sTemp As String

Const PatternDate As String = "^\d{4}-\d{2}-\d{2}\b"
Const PatternF As String = "(" & PatternDate & _
")?" & "((^|\s)(F\d{1,2}(,|$))*)?"
'Not good form to make both parts of the regex _
optional, but it'll work because of other code below

For Each c In Selection
strDate = Run([regex.mid], c.Text, PatternDate)
ValidEntry = IsDate(strDate) Or Left(c.Text, 1) Like "[Ff]"

If ValidEntry = True Then
strF = Run([regex.mid], c.Text, PatternF, , False)
ValidEntry = (strF = c.Text)
End If
Debug.Print c.Text & " " & ValidEntry

If ValidEntry = True Then
If strDate < "" Then
c.Offset(0, 1).Value = strDate
c.Offset(0, 1).NumberFormat = "yyyy-mm-dd"
End If

For i = 1 To Run([regex.count], strF, "F\d+", False)
c.Offset(0, i + 1).Value = Run([regex.mid], strF, "F\d+", i, False)
Next i
Else
c.Offset(0, 1).Value = "Invalid Entry"
End If
Next c

End Sub
============================================

--ron