在我的数据分析的持续传奇(First Question)中,我想删除部门(字段7)不是101,102或103的所有行(名称已被更改以保护无辜者) 。数据中有大约一百个部门,因此使用Criteria1:=Array("104", "105", "106",
等是不切实际的。
我想做这样的事情:
myrange.AutoFilter Field:=7, Criteria1:="<>101", Operator:=xlOr, Criteria2:="<>102", Operator:=xlOr, Criteria3:="<>103"
但Excel无法识别超过2个条件。我可以添加一个帮助列,并让宏遍历每一行(如果101,102或103,然后值=是),过滤掉所有剩余的,并删除所有剩余的,但我将其保存为最后一行胜地。
Autofilter Criteria1是否有办法让阵列不等于?类似的东西:
myrange.AutoFilter Field:=7, Criteria1:="<>" & Array("101", "102", "103")
答案 0 :(得分:4)
记住目标是删除不匹配的行; AutoFilter只是帮助实现目标的一种工具。如果AutoFilter无法满足您的需求,请选择其他方法。考虑:
Sub AllBut()
Dim rTable As Range, r As Range
Dim rDelete As Range
Set rTable = Selection
Set rDelete = Nothing
For Each r In rTable.Columns(7).Cells
v = r.Value
If v <> "101" And v <> "102" And v <> "103" Then
If rDelete Is Nothing Then
Set rDelete = r
Else
Set rDelete = Union(r, rDelete)
End If
End If
Next
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub
这里我们选择要处理的数据块(不包括标题行)。宏扫描该块的第7列并删除任何与该条件不匹配的行。
剩下的只有101's,102's和103's。
答案 1 :(得分:2)
由于这是关于AutoFilter method,我将提供这种方法,包括使用Scripting.Dictionary object来模仿在工作表上手动执行的过程。
在工作表上,用户将应用自动筛选,然后使用列G的下拉菜单“关闭”101,102和103值。剩下的东西将被删除。在VBA中,我们可以获取所有列G并使用非101,102或103的值填充字典对象,并将其用作过滤操作的条件。
Sub filterNotThree()
Dim d As Long, dDELs As Object, vVALs As Variant
Set dDELs = CreateObject("Scripting.Dictionary")
With Worksheets("Sheet6")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
'grab all of column G (minus the header) into a variant array
vVALs = .Resize(.Rows.Count - 1, 1).Offset(1, 6).Value2
'populate the dictionary object with the values that are NOT 101, 102, or 103
For d = LBound(vVALs, 1) To UBound(vVALs, 1)
Select Case vVALs(d, 1)
Case 101, 102, 103
'do not add
Case Else
'not a match, add it to the delete list
'the AutoFilter criteria needs to be text
' so we set the Keys as text and the Items as numbers
dDELs.Item(CStr(vVALs(d, 1))) = vVALs(d, 1)
End Select
Next d
'check to make sure there is something to filter on
If CBool(dDELs.Count) Then
'filter on the dictionary keys
.AutoFilter field:=7, Criteria1:=dDELs.keys, Operator:=xlFilterValues
'delete the visible rows (there has to be some)
.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).EntireRow.Delete
End If
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
dDELs.RemoveAll: Set dDELs = Nothing
End Sub
答案 2 :(得分:0)
我做了类似的事情,但有两个字段,这个语法对我有用:
Exception Value: no such table: polls_question_page
希望它有所帮助。
答案 3 :(得分:0)
我知道这已经晚了,但如果您需要 2 个以上的条件,则必须使用数组。
from sagemaker.predictor import Predictor
predictor = Predictor(endpoint_name="YOUR-ENDPOINT-NAME", sagemaker_session=sagemaker_session_object)
predictor.update_endpoint(instance_type="ml.t2.large", initial_instance_count=1)