当前,我正在尝试从标题为“ Current Ac”的列中删除所有显示0和0.00的值。该代码应查找列标题Current Ac,选择整个数据范围(可以变化),然后在“ Current Ac”列中过滤值0和0.00,然后删除这些行。
到目前为止,对于以下代码行,我已经收到了相应的错误消息:
x = HQSL.Application.Match("Current Ac", Range("A1", Cells(1, LastCol)), 0)
运行时错误'13':类型不匹配
和
.AutoFilter Field:=13, Criteria1:="0", Operator:=xlOr, Criteria2:="0.00"
运行时错误“ 424”:需要对象
我尝试Dim X As Variant
,LastCol
和LastRow As Long
,但仍然收到相同的错误。
我也尝试将所有Dim
都设为Variant
。
Sub FilteredAndDeletedZeros1()
Dim x As Double
Dim LastCol As Variant
Dim LastRow As Variant
Dim HQSL As Worksheet
Set HQSL = ThisWorkbook.Worksheets("HQ SL")
LastCol = HQSL.Cells(1, HQSL.Columns.Count).End(xlToLeft).Column
LastRow = HQSL.Cells(HQSL.Rows.Count, "A").End(xlUp).Row
x = HQSL.Application.Match("Current Ac", Range("A1", Cells(1, LastCol)), 0)
Application.ScreenUpdating = False
With HQSL.Range(Cells(1, x), Cells(LastRow, x)).Value2
.AutoFilter Field:=13, Criteria1:="0", Operator:=xlOr, Criteria2:="0.00"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
Application.ScreenUpdating = True
ChangeColumnNames1
End Sub
答案 0 :(得分:0)
此行
With HQSL.Range(Cells(1, x), Cells(LastRow, x)).Value2
需要更改为
With HQSL.Range(HQSL.Cells(1, x), HQSL.Cells(LastRow, x))
这行
x = HQSL.Application.Match("Current Ac", Range("A1", Cells(1, LastCol)), 0)
需要更改为
x = Application.Match("Current Ac", HQSL.Range("A1", HQSL.Cells(1, LastCol)), 0)
此外,您还必须测试匹配结果是否为错误或成功If Not IsError(x) Then
,否则会导致异常。
最后它应该看起来像这样:
Sub FilteredAndDeletedZeros1()
Dim HQSL As Worksheet
Set HQSL = ThisWorkbook.Worksheets("HQ SL")
Dim LastCol As Long
LastCol = HQSL.Cells(1, HQSL.Columns.Count).End(xlToLeft).Column
Dim LastRow As Long
LastRow = HQSL.Cells(HQSL.Rows.Count, "A").End(xlUp).Row
Dim x As Double
x = Application.Match("Current Ac", HQSL.Range("A1", HQSL.Cells(1, LastCol)), 0)
If Not IsError(x) Then 'test if match was successfull
Application.ScreenUpdating = False
With HQSL.Range(HQSL.Cells(1, x), HQSL.Cells(LastRow, x))
.AutoFilter Field:=13, Criteria1:="0", Operator:=xlOr, Criteria2:="0.00"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
Application.ScreenUpdating = True
Else
MsgBox "Match failed to find something"
End If
ChangeColumnNames1
End Sub
答案 1 :(得分:-1)
范围函数可以用作
Range("A1:A100") 'Range of cells
或
Range(cells(1,1),cells(1,2), ... , Cells(1,100)) 'Individual Cells
您遇到的第一个错误是由于
Range("A1", Cells(1, LastCol))
您正在混合使用这两种类型,即类型不匹配
第二个您尝试对两个单元格的值进行排序
HQSL.Range(Cells(1, x), Cells(LastRow, x)).Value2
建议您同时尝试使用
Range("A1:" & cells(1,LastCol).address) )