VBA联合未按预期工作
我的代码不起作用。它在VBA联合未按预期工作,vba,excel,for-loop,union,Vba,Excel,For Loop,Union,我的代码不起作用。它在uni.entirerow.delete行中断,出现错误1004。这表明它正在构建数组,但我一定是引用了错误的内容?我最初只是让它删除line=by=line,但它需要太长的时间,大约有60万行需要处理 如果列B的值等于ws2中p的任何值,则宏应该引用。如果是,请将其添加到uni,然后在全面查看范围后删除。联合应该比逐行删除快得多 有什么想法吗?完整代码如下: Option Explicit Sub TrimOut() Dim wb As Workbook Dim ws1
uni.entirerow.delete
行中断,出现错误1004。这表明它正在构建数组,但我一定是引用了错误的内容?我最初只是让它删除line=by=line,但它需要太长的时间,大约有60万行需要处理
如果列B的值等于ws2
中p
的任何值,则宏应该引用。如果是,请将其添加到uni
,然后在全面查看范围后删除。联合应该比逐行删除快得多
有什么想法吗?完整代码如下:
Option Explicit
Sub TrimOut()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long, p As Long
Dim uni As Range
Application.Calculation = xlCalculationManual
Set wb = ThisWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
For i = 610197 To 591043 Step -1
For p = 8 To 82
If ws1.Range("B" & i).Value = ws2.Range("A" & p).Value Then
'ws1.Rows(i).Delete old snippet, works fine
If uni Is Nothing Then
Set uni = ws1.Cells(i, 1).EntireRow
Else
Set uni = Application.Union(uni, ws1.Cells(i, 1).EntireRow)
End If
End If
Next p
Next i
If Not uni Is Nothing Then
uni.EntireRow.Delete
End If
Application.Calculation = xlCalculationAutomatic
End Sub
编辑:这是一个“工作”代码,虽然不是最佳的
Option Explicit
Sub TrimOut()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long, p As Long
Dim uni As Range
Dim count As Long
Application.Calculation = xlCalculationManual
Set wb = ThisWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
count = 0
For i = 607257 To 1 Step -1
For p = 8 To 82
If ws1.Range("B" & i).Value = ws2.Range("A" & p).Value Then
'ws1.Rows(i).Delete
If uni Is Nothing Then
Set uni = ws1.Rows(i)
Else
Set uni = Excel.Union(uni, ws1.Rows(i))
count = count + 1
End If
End If
If count > 1000 Then
uni.Delete
count = 0
End If
Next p
Next i
If Not uni Is Nothing Then
uni.Delete
End If
Application.Calculation = xlCalculationAutomatic
End Sub
我刚刚运行了你的代码,看起来慢的不是删除,而是嵌套循环。您的迭代次数超过140万次((610197-591043)*(82-8)),并且每次都可能更改范围。这通常很慢。考虑另一种方法,例如,在数组或列表中记录数据,然后进一步尝试
要查看您自己,请在
下一个i
之后的行中写入停止
,并查看它到达的速度。然后删除速度相当快。我已经成功运行了下面的代码。对于一些不同的报告,我需要查看一些指标,不包括p=1到958的“前5名”、“前10名”等。谢谢大家的帮助
“真正”的问题似乎是excel不想删除未分组的行组,并抛出了一个绝对的嘶嘶声
将count循环移到p之前可能是最佳选择,但无论如何
Option Explicit
Sub TrimOut()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long, p As Long
Dim uni As Range
Dim count As Long
Dim lrow As Long
Application.Calculation = xlCalculationManual
Set wb = ThisWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
lrow = ws1.Range("A" & ws1.Rows.count).End(xlUp).Row
For i = lrow To 1 Step -1
For p = 1 To 82
If Not uni Is Nothing Then
If count > i + 1 Then
uni.Delete
Set uni = Nothing
End If
End If
If ws1.Range("B" & i).Value = ws2.Range("A" & p).Value Then
If uni Is Nothing Then
Set uni = ws1.Rows(i)
Else
Set uni = Excel.Union(uni, ws1.Rows(i))
End If
count = i
End If
Next p
Next i
If Not uni Is Nothing Then
uni.Delete
End If
Application.Calculation = xlCalculationAutomatic
End Sub
你在uni.entirerow.delete之前检查过uni.address
了吗?看看是否有任何东西绑定到uni
?@Rosetta我在uni.delete
周围添加了一个如果没有,uni.delete
,我相信你是这么建议的,仍然存在错误1004您能给出错误1004的完整描述吗?我在这里胡乱猜测,尝试不删除,而是执行uni。选择,然后返回工作表手动删除行,查看是否“对于Excel来说,执行操作太复杂”。啊,这太混乱了。请将“问题”保留为“问题”,并将答案作为“答案”发布-不要编辑问题以包含解决方案,这不是问答的工作方式。那么有更好的方法吗?@MattCottrill-您可以尝试一下,将行记录在列表中,而不是创建一个联合。然后关闭动画并删除这些行。它可能更快,但我不确定。似乎一个自动筛选和一组可见单元格可以在没有任何循环的情况下做到这一点。@Mat'smugh您是正确的,我可以从筛选中删除值,但我需要将数据与我正在构建的实际报告的sumifs进行梳理,我不相信我可以合并sumif(仅可见)?