2015-12-02 124 views
1

我看不到我在哪裏出錯,任何幫助表示讚賞。 我試圖剪切並粘貼其中有'解決'這個詞的任何行到另一個電子表格,但是代碼在循環開始時被阻塞在cl.activate上。錯誤1004選擇範圍級別的方法失敗

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

您需要激活表第一,用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 
+0

或者只是使用'cl.EntireRow.Cut'和完全跳過激活。 (否則,你也需要先激活正確的工作簿) – Rory

+0

@Rory:的確,我只是停止了激活問題,並沒有閱讀下一行...... 但是,如果OP不需要,這是一個更好的命題使用它(我這樣做「調試」很容易) – R3uK

+0

非常感謝你們,都工作得很好。非常感激。 –

相關問題