2014-03-28 96 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