VBA复制单元格(如果包含字符串)

时间:2019-02-21 12:56:36

标签: excel vba

我正在尝试编写一个宏,如果它们包含字符串,它将复制工作表中的所有单元格。例如,如果单元格包含 www.ebay.com (可以是更长字符串的一部分,例如 https://www.ebay.com/itm/Portable-USB-Digital-MP3-Music-Player-LCD-Screen-Support-32GB-TF-Card-FM-Radio/121960292248 ),则添加到剪贴板。

1 个答案:

答案 0 :(得分:1)

您可以修改此代码并尝试。

Option Explicit

Sub test()

    Dim Lastrow As Long, i As Long
    Dim strToSearch As String
    Dim obj As Object

    Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    With ThisWorkbook.Worksheets("Sheet1")

        strToSearch = "Ebay"

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To Lastrow
            'Have in mind that InStr is case sensitive.
            If InStr(1, .Range("A" & i).Value, strToSearch) > 0 Then
                'Just Copy
                .Range("A" & i).Copy
                'Put in clipboard
                obj.SetText .Range("A" & i).Value
                obj.PutInClipboard
                Set obj = Nothing
            End If
        Next i

    End With

End Sub