I had this code working, then tried to add a loop, to work over several sheets, which I failed, so tried to return to the original version, and now i can't get that working either. I have tried just about evere iteration i can see of where to put the with/end with and if/end if but now i am lost. Can anyone see what i have done wrong?
我有这个代码工作,然后尝试添加一个循环,来处理几个表,我失败了,所以试图回到原始版本,现在我也无法让它工作。我已经尝试了一下evere迭代,我可以看到在哪里放置with / end和if / end 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 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
This should get your original code back up and running. You just had your End With
and End If
's a bit out of order.
这应该可以恢复并运行原始代码。你只有你的结束和结束如果有点不正常。
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
Now, to loop through each sheet try wrapping your code with this block: (change sheet names to desired sheets to loop through)
现在,要遍历每个工作表,尝试使用此块包装代码:(将工作表名称更改为所需的工作表以循环)
For Each ws In Sheets(Array("OPT 1 Total", "Sheet2", "Sheet3"))
'your code to loop here
Next
Full code: (change sheet names to desired sheets to loop through)
完整代码:(将工作表名称更改为要循环的所需工作表)
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
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/03/28/d356d8e4aa1f132e5fa25cd1caf70e92.html。