2017-07-26 44 views
0

我試圖從所有工作表中刪除不需要的行。例如: 我有多個工作表,我需要做一個治療,包括找到一個像「 - 」這樣的特殊字符,並刪除所有高於該行的所有行,並且我的意思是排除所有行。調整具有相同佈局的所有工作表

我現在被卡住了..我無法正確地將它應用到我的所有工作表中,並且細胞範圍每天都有所不同。

到目前爲止,我所能做的..

Sub Clean() 
' 
' Clean Macro 
' 

' 
    Cells.Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort.SortFields.Add Key _ 
     :=Range("A1:A66723"), SortOn:=xlSortOnValues, Order:=xlAscending, _ 
     DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Columns("A:A").Select 
    Selection.Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Activate 
    Range("A59044").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.EntireRow.Delete 
    Selection.End(xlUp).Select 
    Selection.End(xlUp).Select 
End Sub 
+0

因此,在A列,你想找到一個細胞,只有'-',然後刪除它上面的所有行?然後重複工作簿中的所有工作表? – BruceWayne

+0

像@BruceWayne我不完全確定你在找什麼。但是,如果我瞭解您的問題,可以參考以下幾個方面:查看'offset'以及如何動態獲取工作表中的最後一行 – Zac

+0

是的。 我需要在A列中找到並重復所有工作表。@ BruceWayne – ARGC

回答

0

這個工作對我來說:

Sub Clean() 
    Dim ws As Worksheet 
    Dim Search As Range 
    Dim addr As String 

    For Each ws In Worksheets 
     ws.Activate 

     With ws.Sort 
      .SortFields.Clear 
      .SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
      .SetRange Cells 
      .Header = xlNo 
      .MatchCase = False 
      .Orientation = xlTopToBottom 
      .SortMethod = xlPinYin 
      .Apply 
     End With 


     With ws.Range("A:A") 
      Set Search = .Find(What:="-", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext) 

      If Not Search Is Nothing Then 
       addr = Search.Address 
      Else 
       Exit Sub 
      End If 
     End With 

     Range(addr).Select 
     ActiveCell.Offset(1, 0).Select 
     Range(ActiveCell, ActiveCell.End(xlDown)).Select 
     Selection.EntireRow.Delete 

    Next 

End Sub 
+0

沒有工作 – ARGC

+0

它給出了一些錯誤?或根本沒有工作? @ARGC –

+0

>對象變量或未設置塊變量(錯誤91) – ARGC

相關問題