简体   繁体   中英

Macro to Copy Cells from one Column to the Clipboard if Another Column Equals Value

I have a large list of contacts and I want a macro that copies their email addresses, Column J, to the clipboard if they've been selected, ="a" in Column C.

The contact list is constantly edited with some being deleted and others added. I've named the row above and below the data information for function reference which has been working well with my other macros.

Sub CopySelected()
Dim oData As New DataObject 
oData.SetText Text:=Empty 'Clear
oData.PutInClipboard
With Worksheets("Master")
    For Each Cell In Range(.Cells(.Range("BorderFirstRow").Row + 1, "C"), _
                .Cells(.Range("BorderLastRow").Row - 1, "C"))
        If Cell.Value = "a" Then

                .PutInClipboard
        End If
    End With
End Sub

What do I need to do to have the macro copy the email address, Column J, if Column C="A"?

This is a fictitious worksheet with fictitious data. Hope it is like yours: 在此处输入图片说明 The code below gave no errors for me:

Sub CopyToClip()

Dim ClipB As New DataObject
Dim RangeToConsider As Range
Dim strAddresses As String

    ' set the range
    ' here just set the relevant range from the C column
        Set RangeToConsider = Range("E4:E7") ' in my case!
For Each cell In RangeToConsider
    If cell.Value = "a" Or cell.Value = "A" Then
        If Not (Trim(cell.Offset(0, -2).Value) = "") Then
           strAddresses = strAddresses & "; " & cell.Offset(0, -2).Value
        end if
    End If
Next
strAddresses = Mid(strAddresses, 2) ' delete the first semicolon
strAddresses = strAddresses & ";" ' add a semicolon at the end
strAddresses = Trim(strAddresses) ' delete spaces if any
Debug.Print strAddresses
ClipB.SetText strAddresses
ClipB.PutInClipboard
Debug.Print ClipB.GetText()
End Sub

So, after running the procedure, I am able to paste 'Name; AnotherName;'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM