2014-03-28 97 views
0

我有這段代碼工作,然後試圖添加一個循環,工作在幾張,我失敗了,所以試圖返回到原來的版本,現在我不能得到那個工作無論是。 我已經嘗試了幾次迭代,我可以看到在哪裏放置/結束,如果/結束,如果但現在我迷路了。 任何人都可以看到我做錯了什麼?如果塊和塊混淆

Sub Applyfilter() 
    Dim ws As Worksheet 
    Dim LastRowColA As Long 
    Dim sCell As Range, lstCell As Range, filterRng As Range 
    Dim i As Integer 

    Set ws = ThisWorkbook.Sheets("OPT 1 Total") 

    With ws 
    Set sCell = .Cells.Find(What:="WFE", LookAt:=xlWhole) 

    If Not sCell Is Nothing Then 
     Set lstCell = .Cells(.Rows.Count, sCell.Column).End(xlUp) 

     If lstCell.Row > 1 Then 
     'Debug.Print sCell, lstCell 
    End With 

    Range("A1").Select 
    Selection.End(xlToRight).Select ' select all cols from A to last populated 
    Selection.AutoFilter 
    ActiveSheet.AutoFilter.Sort.SortFields.Clear 

    ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _ 
    Range(sCell, lstCell), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _ 
    :=xlSortNormal 

    With ActiveSheet.AutoFilter.Sort 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
    End With 

    End If 

    End If 

    Set filterRng = Range("A2").CurrentRegion 
    i = Application.WorksheetFunction.Match("WFE", Range("A1:AZ1"), 0) 

    'Set filter to only look for WFE greater than 0.5 
    filterRng.AutoFilter Field:=i, Criteria1:=">=0.5" _ 
    , Operator:=xlAnd 

    End If 
End Sub 
+0

你的問題非常含糊......什麼具體不工作? – 2014-03-28 10:29:16

+0

對於初學者來說,你的'End If'不合適。 –

回答

0

這應該讓您的原始代碼備份和運行。你只是讓你的End WithEnd If有點失靈。現在

Sub Applyfilter() 
    Dim ws As Worksheet 
    Dim LastRowColA As Long 
    Dim sCell As Range, lstCell As Range, filterRng As Range 
    Dim i As Integer 

    Set ws = ThisWorkbook.Sheets("OPT 1 Total") 

    With ws 
    Set sCell = .Cells.Find(What:="WFE", LookAt:=xlWhole) 

    If Not sCell Is Nothing Then 
     Set lstCell = .Cells(.Rows.Count, sCell.Column).End(xlUp) 

     If lstCell.Row > 1 Then 
      'Debug.Print sCell, lstCell 
     End If 

     Range("A1").Select 
     Selection.End(xlToRight).Select ' select all cols from A to last populated 
     Selection.AutoFilter 
     ActiveSheet.AutoFilter.Sort.SortFields.Clear 

     ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _ 
     Range(sCell, lstCell), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _ 
     :=xlSortNormal 

    End If 
    End With 

    With ActiveSheet.AutoFilter.Sort 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
    End With 

    Set filterRng = Range("A2").CurrentRegion 
    i = Application.Match("WFE", Range("A1:AZ1"), 0) 

    'Set filter to only look for WFE greater than 0.5 
    filterRng.AutoFilter Field:=i, Criteria1:=">=0.5" _ 
    , Operator:=xlAnd 
End Sub 

,通過每個片材循環嘗試與此塊包裹您的代碼:(通過改變片材名稱,以所希望的片材循環)

For Each ws In Sheets(Array("OPT 1 Total", "Sheet2", "Sheet3")) 
    'your code to loop here 
Next 

完整代碼:(將表格名稱更改爲所需的表格以循環瀏覽)

Sub ApplyfilterLoop() 
    Dim ws As Worksheet 
    Dim LastRowColA As Long 
    Dim sCell As Range, lstCell As Range, filterRng As Range 
    Dim i As Integer 

    For Each ws In Sheets(Array("OPT 1 Total", "Sheet2")) 
     With ws 
      Set sCell = .Cells.Find(What:="WFE", LookAt:=xlWhole) 

      If Not sCell Is Nothing Then 
      Set lstCell = .Cells(.Rows.Count, sCell.Column).End(xlUp) 

      If lstCell.Row > 1 Then 
        'Debug.Print sCell, lstCell 
      End If 

      ws.Activate 
      ws.Range("A1").Select 
      Selection.End(xlToRight).Select ' select all cols from A to last populated 
      Selection.AutoFilter 
      ws.AutoFilter.Sort.SortFields.Clear 

      ws.AutoFilter.Sort.SortFields.Add Key:= _ 
       Range(sCell, lstCell), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _ 
       :=xlSortNormal 

      End If 
     End With 

     With ws.AutoFilter.Sort 
      .Header = xlYes 
      .MatchCase = False 
      .Orientation = xlTopToBottom 
      .SortMethod = xlPinYin 
      .Apply 
     End With 

     Set filterRng = ws.Range("A2").CurrentRegion 
     i = Application.Match("WFE", ws.Range("A1:AZ1"), 0) 

     'Set filter to only look for WFE greater than 0.5 
     filterRng.AutoFilter Field:=i, Criteria1:=">=0.5", Operator:=xlAnd 
    Next 
End Sub 
+0

謝謝!!!!!原始版本的作品,但循環不?它會卡住「ActiveSheet.AutoFilter.Sort.SortFields.Clear」err91對象變量或未設置塊。我試圖使它成爲「ws」,而不是ActiveSheet,但這並不奏效,任何想法 – user3432849

+0

啊......我應該抓住這個。你用正確的方式替換了ws。看我的編輯。我用一個簡單的例子來測試它。 –

+0

謝謝,工作正常。如果我想用更動態的東西替換數組,例如,如果ws.Name像「* OPT * Total」那麼...我得到一個錯誤。我把End If,在Next之前的末尾,也試過了,但是我得到了不同的錯誤。我可以不這樣做嗎? – user3432849