如何提高代码的性能并在更少的时间内执行VBA Excel代码Countif
希望您可以帮助我在VBA Excel中优化此代码块。当我执行少于3万个记录的代码块时,执行需要3分钟。
我希望您的支持能够验证是否有可能提高代码的性能并在更少的时间内执行它。
我该如何改进这条线以使执行时间更少?我希望可以以两个代码块中的任何一个为例。
非常感谢您的支持,
Sub findduplicates()
Dim ws As Worksheet: Set ws = ActiveSheet 'always specify a worksheet
Range("BE1") = "Flag_Unico"
With ws.Range("BE2:BE" & ws.Cells(Rows.count, "N").End(xlUp).Row)
.Formula = "=COUNTIF(BD:BD,BD2)=1"
.Value = .Value
End With
End Sub
此代码花了2分钟。17秒执行且它的作用是设置一个真或错误的标志。如果是错误的,它将相同的标志设置为原始标志,并且重复
Sub findduplicates()
Dim ws As Worksheet: Set ws = ActiveSheet 'always specify a worksheet
Range("BE1") = "Flag_Unico"
With ws.Range("BE2:BE" & ws.Cells(Rows.count, "N").End(xlUp).Row)
.Formula = "=IF(COUNTIF(BD:BD,BD2)=1,0,1)"
.Value = .Value
End With
End Sub
此代码花费了2分钟。08秒执行,并设置1或0标志。如果是0,它将相同的标志设置为原始标志和副本
I hope you can help me to optimize this code block in VBA EXCEL. When I execute the block of code with less than 30 thousand records, it takes 3 minutes to execute.
I want your support to validate if there is a possibility to improve the performance of the code and to execute it in less time.
How could I improve that line so that it takes less time to execute? I hope that either of the two blocks of code can be taken as an example.
Thank you very much for your support
Sub findduplicates()
Dim ws As Worksheet: Set ws = ActiveSheet 'always specify a worksheet
Range("BE1") = "Flag_Unico"
With ws.Range("BE2:BE" & ws.Cells(Rows.count, "N").End(xlUp).Row)
.Formula = "=COUNTIF(BD:BD,BD2)=1"
.Value = .Value
End With
End Sub
This code took '2 min.17 sec to execute and what it does is set a TRUE or FALSE flag. If it is FALSE, it sets the same FLAG to the original and the duplicate
Sub findduplicates()
Dim ws As Worksheet: Set ws = ActiveSheet 'always specify a worksheet
Range("BE1") = "Flag_Unico"
With ws.Range("BE2:BE" & ws.Cells(Rows.count, "N").End(xlUp).Row)
.Formula = "=IF(COUNTIF(BD:BD,BD2)=1,0,1)"
.Value = .Value
End With
End Sub
This code took '2 min.08 sec to execute and what it does is set a 1 or 0 flag. If it is 0, it sets the same FLAG to the original and the duplicate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标志唯一值
Flag Unique Values
拜托,请尝试下一步。它必须使用数组非常快,并且仅在内存中工作,并且
字典
以识别唯一的情况。它将仅在下一次发生(第二,第三,第四,依此类推...)时将标志放置。通过这种方式,它提供了通过标志和删除重复项进行排序的可能性,仅剩下独特的情况:Please, try the next way. It must be very fast using arrays and working only in memory, and a
Dictionary
to identify the unique cases. It will place a flag only for the next occurrence (second, third, fourth and so on...). In this way it offers the possibility to sort by flag and delete duplicates, only unique cases remaining: