Guest User

Untitled

a guest
Jun 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. Function simpleCellRegex(Myrange As Range) As String
  2. Dim regEx As Object
  3. Set regEx = CreateObject("VBScript.RegExp")
  4. Dim strPattern As String
  5. Dim strInput As String
  6. Dim strReplace As String
  7. Dim strOutput As String
  8.  
  9.  
  10. strPattern = "[Γ -ΓΏ]+"
  11.  
  12. If strPattern <> "" Then
  13. strInput = Myrange.Value
  14. strReplace = ""
  15.  
  16. With regEx
  17. .Global = True
  18. .MultiLine = True
  19. .IgnoreCase = False
  20. .Pattern = strPattern
  21. End With
  22.  
  23. If regEx.Test(strInput) Then
  24. simpleCellRegex = regEx.Replace(strInput, strReplace)
  25. Else
  26. simpleCellRegex = "Not matched"
  27. End If
  28. End If
  29. End Function
  30. '//////////////////////////////////////////////////////////////
  31. Private Sub simpleRegex()
  32. Dim strPattern As String: strPattern = "^[0-9]{1,2}"
  33. Dim strReplace As String: strReplace = ""
  34. Dim regEx As New RegExp
  35. Dim strInput As String
  36. Dim Myrange As Range
  37.  
  38. Set Myrange = ActiveSheet.Range("A1")
  39.  
  40. If strPattern <> "" Then
  41. strInput = Myrange.Value
  42.  
  43. With regEx
  44. .Global = True
  45. .MultiLine = True
  46. .IgnoreCase = False
  47. .Pattern = strPattern
  48. End With
  49.  
  50. If regEx.Test(strInput) Then
  51. MsgBox (regEx.Replace(strInput, strReplace))
  52. Else
  53. MsgBox ("Not matched")
  54. End If
  55. End If
  56. End Sub
  57. '//////////////////////////////////////
  58. 'This example is the same as example 1 but loops through a range of cells.
  59. 'Example 3: Loop Through Range
  60. '////////////////////////
  61. Private Sub simpleRegex()
  62. Dim strPattern As String: strPattern = "^[0-9]{1,2}"
  63. Dim strReplace As String: strReplace = ""
  64. Dim regEx As New RegExp
  65. Dim strInput As String
  66. Dim Myrange As Range
  67.  
  68. Set Myrange = ActiveSheet.Range("A1:A5")
  69.  
  70. For Each cell In Myrange
  71. If strPattern <> "" Then
  72. strInput = cell.Value
  73.  
  74. With regEx
  75. .Global = True
  76. .MultiLine = True
  77. .IgnoreCase = False
  78. .Pattern = strPattern
  79. End With
  80.  
  81. If regEx.Test(strInput) Then
  82. MsgBox (regEx.Replace(strInput, strReplace))
  83. Else
  84. MsgBox ("Not matched")
  85. End If
  86. End If
  87. Next
  88. End Sub
  89. '///////////////////////////////////////////////////////
  90. 'Example 4: Splitting apart different patterns
  91. 'This example loops through a range (A1, A2 & A3) and looks for a string starting with three digits followed by a single alpha character and then 4 numeric digits. The output splits apart the pattern matches into adjacent cells by using the (). $1 represents the first pattern matched within the first set of ().
  92. '/////////////////////////////////////////////////////////
  93. Private Sub splitUpRegexPattern()
  94. Dim regEx As New RegExp
  95. Dim strPattern As String
  96. Dim strInput As String
  97. Dim Myrange As Range
  98.  
  99. Set Myrange = ActiveSheet.Range("A1:A3")
  100.  
  101. For Each C In Myrange
  102. strPattern = "(^[0-9]{3})([a-zA-Z])([0-9]{4})"
  103.  
  104. If strPattern <> "" Then
  105. strInput = C.Value
  106.  
  107. With regEx
  108. .Global = True
  109. .MultiLine = True
  110. .IgnoreCase = False
  111. .Pattern = strPattern
  112. End With
  113.  
  114. If regEx.test(strInput) Then
  115. C.Offset(0, 1) = regEx.Replace(strInput, "$1")
  116. C.Offset(0, 2) = regEx.Replace(strInput, "$2")
  117. C.Offset(0, 3) = regEx.Replace(strInput, "$3")
  118. Else
  119. C.Offset(0, 1) = "(Not matched)"
  120. End If
  121. End If
  122. Next
  123. End Sub
Add Comment
Please, Sign In to add comment