I am trying to create a macro that will copy the valid email addresses in a column to the clipboard on button click, ignoring any invalid ones. I am completely new to VBA and as such I am having some difficulties. I've looked all over the internet and stack exchange and this is what I have been able to come up with so far:
Private Sub CommandButton1_Click()
Dim clipboard As MSForms.DataObject
Dim Emails As String
Set r = Intersect(Range("B1").EntireColumn, ActiveSheet.UsedRange)
For Each i In r
If Trim(i) Like "?*@[!.]*.[!.]*" Then
If Not i Like "*@*@*" Then
Emails = Emails & i
End If
End If
Next i
clipboard.SetText Emails
clipboard.PutInClipboard
End Sub
This code is supposed to evaluate each cell in a column to determine if the email address is valid, and if it is valid, append the email address to the String Emails. Once finished, the String will be copied to the clipboard so that it can be pasted in the "To" line of an email client (ie, Outlook). I've also considered other solutions, such as adding all valid email addresses to an array, but it seemed more complicated to copy an array to the clipboard. Either way if there is a more elegant solution, I'm all for it. Any pointers are appreciated!