title: Copy and paste cell to the next column based on a condition
body: <p>I am trying to paste the cell contents based on a condition that if there is no match then copy the first word of the cell and paste it to the next cell to the right but it gives me object not defined error.</p>
  
  <p>CENTRUM ADVANCE TABLET should copy only CENTRUM</p>
  
  <p>Below is my code</p>
  
  <pre><code>Sub splitUpRegexPattern()
  
  Dim re As Object, c As Range
  Dim allMatches
  Dim cell As Object
  Dim count As Integer
  count = 0
  
  For Each cell In Selection
      count = count + 1
  Next cell
  ' MsgBox count &amp; " item(s) selected"
  
  Set re = CreateObject("VBScript.RegExp")
  re.Pattern = "((\d+(?:\.\d+)?)\s*(m?g|mcg|ml|IU|MIU|mgs|µg|gm|microg|microgram)\b)"
  re.IgnoreCase = True
  re.Global = True
  
  For Each c In ActiveSheet.Range("D2", ActiveSheet.Range("D2").End(xlDown)).Cells ' Select the range and run the code
      Set allMatches = re.Execute(c.Value)
      If allMatches.count &gt; 0 Then
          c.Offset(0, 1).Value = allMatches(0)
      Else
          Selection.Copy
          c.Offset(0, 1).Value.Paste
      End If
  Next c
  End Sub
  </code></pre>