如果另一列等于值,将单元格从一列复制到剪贴板的宏

时间:2019-01-31 14:03:19

标签: excel vba

我的联系人列表很大,我想要一个宏,用于将其电子邮件地址(列J)复制到剪贴板(如果已选择)(列C中的=“ a”)。

联系人列表不断进行编辑,其中一些被删除,另一些被添加。我将数据信息的上方和下方的行命名为函数引用,该行与我的其他宏配合得很好。

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

如果列C =“ A”,宏需要如何复制电子邮件地址列J?

1 个答案:

答案 0 :(得分:0)

这是一个包含虚拟数据的虚拟工作表。希望它像你一样: enter image description here  下面的代码对我来说没有错误:

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

因此,在运行该过程之后,我可以粘贴“名称; AnotherName;'