对象所需的excel vba?
所以我有一个电子表格,我试图使它在任何给定的行中,如果列D中的单元格为空,但列a中的单元格大于1,它将删除该行。然而,当我尝试运行它时,我得到了“objectrequired”对象所需的excel vba?,vba,excel,Vba,Excel,所以我有一个电子表格,我试图使它在任何给定的行中,如果列D中的单元格为空,但列a中的单元格大于1,它将删除该行。然而,当我尝试运行它时,我得到了“objectrequired” Sub rowdeletetest() Dim rowBoat As Integer For rowBoat = 7 To 980 If ws.Visible = True Then If IsEmpty(Range("D", rowBoat)) = True And Range("A", rowBoa
Sub rowdeletetest()
Dim rowBoat As Integer
For rowBoat = 7 To 980
If ws.Visible = True Then
If IsEmpty(Range("D", rowBoat)) = True And Range("A", rowBoat) > 1 Then
Rows(rowBoat).EntireRow.Delete
End If
End If
Next rowBoat
End Sub
试试看
当一个函数返回一个布尔值(真/假)时,没有必要为了返回真/假而与真/假进行比较。我想这就是你想要做的:
Sub RowDeleteTest()
Dim rowBoat As Integer
Dim ws As Worksheet
Set ws = ActiveSheet ' change sheet, if required
For rowBoat = 980 to 7 Step - 1
With ws
If .Visible = True Then
If .Cells(4, rowBoat) = VbNullstring And .Cells(1, rowBoat) > 1 Then
.Rows(rowBoat).Delete
End If
End If
End With
Next rowBoat
End Sub
编辑#1
如果要测试公式,VbNullstring
不能正确使用:
Sub RowDeleteTest()
Dim rowBoat As Integer
Dim ws As Worksheet
Set ws = ActiveSheet ' change sheet, if required
For rowBoat = 980 to 7 Step - 1
With ws
If .Visible = True Then
If IsEmpty(.Cells(4, rowBoat)) And .Cells(1, rowBoat) > 1 Then
.Rows(rowBoat).Delete
End If
End If
End With
Next rowBoat
End Sub
哪一行代码抛出了“objectrequired”错误?单击“调试”时,突出显示的是什么?是全局的吗?如果是这样的话,在哪里设置呢?ws实际上最终成为了问题所在。我之前添加了它,因为我将for循环设置为1000,但是工作表不够大,我得到了一个400错误,但是删除它会清除这个错误。谢谢@共产国际感谢回复,但在尝试了这两行之后,我仍然得到了错误。问题最终是ws。无论如何,它不再需要了,它是由于以前的错误而添加的,实际上并没有解决以前的错误。不过谢谢你
=vbNullString
与IsEmpty()
不同。考虑一个包含公式<代码> =“”/COD>的单元格。
Sub RowDeleteTest()
Dim rowBoat As Integer
Dim ws As Worksheet
Set ws = ActiveSheet ' change sheet, if required
For rowBoat = 980 to 7 Step - 1
With ws
If .Visible = True Then
If IsEmpty(.Cells(4, rowBoat)) And .Cells(1, rowBoat) > 1 Then
.Rows(rowBoat).Delete
End If
End If
End With
Next rowBoat
End Sub