代码优化(用于指令) [英] Code Optimization (For instruction)

查看:79
本文介绍了代码优化(用于指令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30 000行和30列的工作表

I've a sheet within 30 000 rows and 30 columns

我需要只保留行它们在两列中具有不同的值。

I need to keep only the rows which have not the same value into two columns.

这些值可以出现在行的任何位置。这就是我要扫描所有行的原因。

Those values can appear anywhere along the rows. That the reason I've to scan all rows.

所以我写了这个简单的代码,但需要一段时间才能达到解决方案。

So I wrote this simple code, but It takes a while to reach the solution.

所以,如果你有更好的方法来优化它,甚至改变所有代码,我将不胜感激。

So, If you have a better way to optimize it, even to change all code, I'll appreciate.

下面的代码:

由于第一行内的标签,我从第2行开始。 Last表示最后一行计数。

I'm starting from line 2 because of the labels inside the first line. Last means the number of the last Row counting.

j = 2 To Last- 1

 适用于k = j + 1至 上次<
   如果Range("A"& j).Value = Range("A"& k).Value和Range("U"& j).Value = Range("U"& k) .Value然后

   行(k&":& k)。选择

    Selection.Delete Shift:= xlUp

    k = k - 1

     Last =  Last -1

结束如果

 下一个k

 下一个j

For j = 2 To Last- 1
  For k = j + 1 To Last
    If Range("A" & j).Value = Range("A" & k).Value And Range("U" & j).Value = Range("U" & k).Value Then
    Rows(k & ":" & k).Select
    Selection.Delete Shift:=xlUp
    k = k - 1
    Last = Last -1
End If
  Next k
 Next j

推荐答案

您可以使用自动过滤器仅显示列中具有相同值的行,然后删除所有可见单元格。我相信这会比迭代30K行更快。 

You could use an autofilter to show only those rows which have the same values in the columns and then delete all the visible cells. I am sure that would be faster than iterating 30K rows. 

还要考虑,禁用事件和屏幕更新以及消除.Select行。

Also consider, disabling events and screen updating as well as eliminating the .Select line.

行(k&":"& k)。删除Shift:= xlUp

Rows(k & ":" & k).Delete Shift:=xlUp


这篇关于代码优化(用于指令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆