我对VBA并不十分精通。我正在尝试创建一个宏,该宏为0和“ N / A”过滤活动工作表的表格。运行宏时,出现错误“ Argument Not Optional”,并返回到ws.Range行。
我查看了其他代码,但不确定自己做错了什么。
Sub Remove_Cleared()
'
' Remove_Rows_Based_On_Value
'
'
Dim ws As Worksheet
Dim activeTable As String
activeTable = ActiveSheet.ListObjects(1).Name
MsgBox activeTable 'Make sure pulling correct table
'Set reference to the sheet in the workbook
Set ws = ActiveSheet
ws.Activate 'not required but allows user to view sheet if warning message
'clear any existing filters
On Error Resume Next
ws.ShowAllData
On Error GoTo 0
'1 Apply Filter
ws.Range.AutoFilter Field:=8, Criteria1:="0", Operator:=xlOr, Criteria2:="N/A"
'2 Delete Rows
Application.DisplayAlerts = False
ws.Range(activeTable).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
'3 Clear Filter
On Error Resume Next
ws.ShowAllData
On Error GoTo 0
End Sub
在宏末尾,我希望从活动工作表的表中删除任何包含0或N / A值的行。
答案 0 :(得分:1)
ws.Range("A1:M300")
... Range
对象需要一个范围。
由于您已经获取了该表的名称,因此可以将其用作参数:
ws.Range(activeTable).AutoFilter Field:=8, Criteria1:="0", Operator:=xlOr, Criteria2:="N/A"