错误 1004 select 范围 class 的方法失败

Error 1004 select method of range class failed

我看不出这里哪里出了问题,感谢您的帮助。 我正在尝试将任何包含单词 'solved' 的行剪切并粘贴到另一个电子表格,但代码在循环开始时卡在 cl.activate 上。

    Sub FindString()
    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim FirstFound As String
    Dim sh As Worksheet

    'Open first item to search and paste destination
    Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\Markerstudy.xlsx"
    Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\solved results.xlsx"
    Workbooks("markerstudy").Activate

    ' Set Search value
    SearchString = "solved"
    Application.FindFormat.Clear
    ' loop through all sheets
    For Each sh In ActiveWorkbook.Worksheets
        ' Find first instance on sheet
        Set cl = sh.Cells.Find(What:=SearchString, _
            After:=sh.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
        If Not cl Is Nothing Then
            ' if found, remember location
            FirstFound = cl.Address
            ' format found cell
            Do
                cl.Activate
                ActiveCell.EntireRow.Cut
                Workbooks("solved results").Activate
                Range("A1").Select
                If ActiveCell <> "" Then
                    ActiveCell.Offset(1, 0).Select
                    ActiveCell.PasteSpecial xlPasteAll
                Else
                    ActiveCell.PasteSpecial xlPasteAll
                End If

                ' find next instance
                Set cl = sh.Cells.FindNext(After:=cl)
                ' repeat until back where we started
            Loop Until FirstFound = cl.Address
        End If
    Next
End Sub

您需要先激活 sheet,然后 cl.Parent.Activate

    Sub FindString()
    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim FirstFound As String
    Dim sh As Worksheet

    'Open first item to search and paste destination
    Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\Markerstudy.xlsx"
    Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\solved results.xlsx"
    Workbooks("markerstudy").Activate

    ' Set Search value
    SearchString = "solved"
    Application.FindFormat.Clear
    ' loop through all sheets
    For Each sh In ActiveWorkbook.Worksheets
        ' Find first instance on sheet
        Set cl = sh.Cells.Find(What:=SearchString, _
            After:=sh.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
        If Not cl Is Nothing Then
            ' if found, remember location
            FirstFound = cl.Address
            ' format found cell
            Do
                cl.Parent.Activate
                cl.Activate
                ActiveCell.EntireRow.Cut
                Workbooks("solved results").Activate
                Range("A1").Select
                If ActiveCell <> "" Then
                    ActiveCell.Offset(1, 0).Select
                    ActiveCell.PasteSpecial xlPasteAll
                Else
                    ActiveCell.PasteSpecial xlPasteAll
                End If

                ' find next instance
                Set cl = sh.Cells.FindNext(After:=cl)
                ' repeat until back where we started
            Loop Until FirstFound = cl.Address
        End If
    Next
End Sub