【发布时间】:2017-03-07 06:03:05
【问题描述】:
该代码旨在删除数据的列标题(在导入多个文件后)。但我得到错误:“1004”这是“应用程序定义或对象定义的错误”。我在 SO 上引用了不同的解决方案,但无济于事。
在我运行此代码片段之前,我删除了空白行并将其包括在内以显示什么也有效,甚至可能掌握关键。
'Remove all rows with blanks first
Set wb = Workbooks(ThisWorkbook.Name)
'Remove blank rows based on if column 'B' is blank as a and B are filled when there is risk key showing
wb.Worksheets("BatchData").Activate
Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Now to delete based on column headers!
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Filter Column A and delete selection
'############# Error line below
ActiveSheet.Range("A1:" & A & ":" & LastRow).AutoFilter Field:=2, Criteria1:= _
"=Item", Operator:=xlOr, Criteria2:="="
Selection.EntireRow.Delete
编辑:
修改了代码,根据 cmets 进行了一些调整,并且我还引用了字段“2”并尝试使用 1 的“A”。
Dim LastRow As Long
LastRow = wb.Worksheets("BatchData").Cells(Rows.Count, 1).End(xlUp).Row
'Filter Column A and delete selection
ActiveSheet.Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:= _
"=Item", Operator:=xlOr, Criteria2:="="
ActiveSheet.Range("$A$1:$A$" & LastRow).Offset(0, 0).SpecialCells _ (xlCellTypeVisible).EntireRow.Delete
最后一行编辑基于; VBA: How to delete filtered rows in Excel? 但偏移量从 1,0 更改为 0,0
【问题讨论】:
-
首先你可以使用
Set wb = ThisWorkbook。其次,在使用LastRow时,最好使用某个工作表引用,例如LastRow = wb.Worksheets("BatchData").Cells(Rows.Count, 1).End(xlUp).Row。另外,将Range("A1:" & A & ":" & LastRow)修改为Range("A1:A" & LastRow) -
我有预感...打印
"A1:" & A & ":" & LastRow并将其添加到您的帖子中。 -
顺便说一句,您删除的是列/行中的范围,而不是整个列/行。
-
行数为 1048576 而 xlUP 为 -4162 有帮助吗?
-
@indofraiser 现在对你有用吗?