我正在尝试遍历我的数据和Union
稍后需要删除的某些行号。下面的代码存储了正确的行,但我无法删除它们。我相信这是因为我的数据排列在表格中,因为如果数据不在表格中,我可以删除所需的行。我收到错误信息'run time error 1004 - delete method of range class failed'
就行了Urng.delete
。
Sub DeleteRows()
Dim ws4 As Worksheet: Set ws4 = Worksheets("Sheet1")
Dim LastRow As Long
Dim CurrentRow As Long
Dim GroupValue
Dim GroupTotal As Long
Dim x As Long
Dim Urng As Range
Application.ScreenUpdating = False
ws4.Activate
GroupValue = ws4.Range("B6").Value
CurrentRow = 6 LastRow = ws4.Cells(Rows.Count, "B").End(xlUp).Row
Set Urng = Rows(LastRow + 1)
For x = 1 To LastRow
GroupTotal = Application.WorksheetFunction.CountIf(Range("B6:B" & LastRow), GroupValue)
If GroupTotal = 1 Then
Set Urng = Union(Urng, Rows(CurrentRow))
End If
CurrentRow = CurrentRow + GroupTotal
GroupValue = Range("B" & CurrentRow).Value
If GroupValue = "" Then '
Exit For
End If
Next x
Urng.Delete
Application.ScreenUpdating = True
End Sub
我试过使用.EntireRow.Delete
没有运气。
表外没有数据,因此仅删除表行可能是一种解决方案,但是,Unions
如果我不能在Union(Urng, Rows(CurrentRow))
.
是否有删除多个整行的 VBA 解决方案,其中行的一部分是表?