错误1004选择范围类的方法失败

时间:2015-12-02 12:49:22

标签: excel vba excel-vba runtime-error

我无法看到我在这里出错的地方,感谢任何帮助。 我试图剪切并粘贴任何包含“已解决”字样的行。在他们的另一个电子表格中,但代码在循环开始时卡在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

1 个答案:

答案 0 :(得分:1)

您需要先使用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