Skip to content

Instantly share code, notes, and snippets.

@capm
Last active January 21, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capm/81907420351e53a11789 to your computer and use it in GitHub Desktop.
Save capm/81907420351e53a11789 to your computer and use it in GitHub Desktop.
Excel VBA Function: Date pattern recognition and replacement.
Function DateMultiFunction(InputDate As String, InputType As Integer) As String
' 0: True or False
' 1: Replace date
' 2: Extract string
' Declare variables
Dim regEx As New RegExp
Dim strPattern As String
'
If InputType = 0 Or InputType = 1 Or InputType = 2 Then
' Date pattern
strPattern = "[0-9]{2}[/][0-9]{2}[/][0-9]{4}"
regEx.Pattern = strPattern
' Comparison function
If InputType = 0 Then
If regEx.Test(InputDate) Then DateMultiFunction = True Else DateMultiFunction = False
End If
' Date replace function
If InputType = 1 Then
If regEx.Test(InputDate) Then DateMultiFunction = regEx.Replace(InputDate, Format(InputDate, "yyyymmdd"))
End If
' String extract function
If InputType = 2 Then
regEx.Pattern = "[0-9]{2}[:][0-9]{2}[:][0-9]{2}"
If regEx.Test(InputDate) Then DateMultiFunction = regEx.Execute(InputDate)(0)
End If
Else
DateMultiFunction = "Wrong InputType"
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment