Last active
January 21, 2016 22:11
-
-
Save capm/81907420351e53a11789 to your computer and use it in GitHub Desktop.
Excel VBA Function: Date pattern recognition and replacement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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