简体   繁体   English

根据动态更新的单元格值删除行的 VBA 代码

[英]VBA code to delete rows based on cell value that update dynamically

I have a sheet where i need to delete rows based on index value of a column which output of formula.我有一张工作表,我需要根据公式输出的列的索引值删除行。

This is the data im working on:这是我正在处理的数据:

在此处输入图片说明

Here's what the data has:这是数据的内容:

Each Job ID (Column A) has 2 values: True and False assigned to different owners每个作业 ID(A 列)有 2 个值:True 和 False 分配给不同的所有者

Now the column F has data that is specified to satisfy certain conditions现在 F 列的数据被指定为满足某些条件

Each time a job ID is deleted (both rows of the job), the last values in F column keep updating to "Yes/No" based on the condition每次删除作业 ID(作业的两行)时,F 列中的最后一个值会根据条件不断更新为“是/否”

What im trying to achieve:我试图实现的目标:

  • To check the rows one at a time and shift/delete two rows (a job) to the bottom when the last column has the value "No"一次检查一行,并在最后一列值为“否”时将两行(一项作业)移动/删除到底部
  • The data updates in the last column as these deletions or shifts happen随着这些删除或移动发生,最后一列中的数据更新
  • So the macro should be dynamic所以宏应该是动态的

Here's the code i've tried:这是我试过的代码:

Sub delete()

    Dim i As Integer
    Dim LastRow As Integer
    LastRow = Range("A").End(xlUp).Row

    For i = 2 To LastRow
        If Range("F" & i).Value = "No" And Range("A" & i).Value = Range("A" & i + 1).Value Then
            Rows(i & ":" & (i + 1)).Cut
            Range("A").End(xlDown).Offset(1, 0).EntireRow.Insert
            Application.CutCopyMode = False
        End If
    Next

End Sub

This doesn't seem to work.这似乎不起作用。 Deletion part has worked partially where i've used the following lines of code:删除部分在我使用以下代码行的情况下部分起作用:

 Rows(i & ":" & (i+1)).Select
        Selection.delete Shift:=x1Up

I don't mean to offend but I don't see the benefits of your system.我无意冒犯,但我看不到你们系统的好处。 Please consider this instead.请考虑这一点。

  1. Create a helper column and enter this formula in row 2 of it.创建一个辅助列并在其第 2 行中输入此公式。 =SUM(INDIRECT(ADDRESS(ROW()-MOD(ROW(),2),5)&":"&ADDRESS(ROW()+1-MOD(ROW(),2),5)))*(IF(AND(INDIRECT(J2)>50,INDIRECT(K2)>50),2,1))+(ROW()/10000)
  2. Copy down to the end of your sheet.复制到工作表的末尾。
  3. Sort on this column.在此列上排序。
  4. Hide the helper.隐藏帮手。

Note that the formula is designed to evaluate two consecutive rows of which the first is in a row with an even number, the second having an uneven number.请注意,该公式旨在评估连续的两行,其中第一行是偶数行,第二行是奇数行。 If your row 2 is used for something else the formula needs to be tweaked to start in row 3 or tweaked differently to start in row 4.如果您的第 2 行用于其他用途,则需要调整公式以从第 3 行开始或以不同方式调整以从第 4 行开始。

And this is how it works.这就是它的工作原理。 It adds the two repetitions (column E).它添加了两次重复(E 列)。 If both of them are above 50 it will multiply the result by two.如果两者都大于 50,则结果将乘以 2。 This will ensure that all rows with two valuations above 50 will be sorted above all others.这将确保两个估值高于 50 的所有行都将排在所有其他行之上。 Finally, the row number / 10000 is added.最后,添加行号 / 10000。 If you have more than 10000 rows in your data make this number 100,000.如果您的数据中有超过 10000 行,则将此数字设为 100,000。 The point is that the result of this division must always be less than 1 so as not to interfere with the valuations.关键是这个除法的结果必须始终小于 1,以免干扰估值。 This addition will work as a tiebreaker when the other valuations are identical and it will ensure that the sequence of your two rows will remain the same after sorting.当其他估值相同时,此添加将用作决胜局,它将确保您的两行的顺序在排序后保持不变。

Of course, this sort will create a list that is much more sensitive than simply moving worse performers to the end.当然,这种排序会创建一个比简单地将表现较差的人移到最后更敏感的列表。 It will also leave your formula in column F undisturbed.它还将使您在 F 列中的公式不受干扰。 It's final advantage is that the criteria in the helper column can be fine tuned to even better serve your purposes.最后一个优点是可以对辅助列中的条件进行微调,以更好地满足您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM