2013-05-13 111 views
2

在Excel中,是否有办法动态更新任务列表的优先级?例如,如果您有一列具有优先级1,5,3,2,4的列,并且用户添加了具有新任务的新行并将其优先级分配为1,则列表将更新为2,6,4,3 ,5,1?在Excel中动态重新排序编号列表

或者还有其他一些(更好的?)的方式吗?我宁愿不必对列表排序,然后在每次添加或删除新任务时手动更改编号。另外,我不认为将使用电子表格的人会这样做。

+0

为什么不只是做一个排序列表,然后将多余的行中的所需位置添加,并把你的新任务,在那里呢? ...它消除了完全编号的需要。 – 2013-05-13 14:44:26

回答

0

看看下面的代码。

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim rngPriorityList As Range 
Dim lNewValue As Long 
Dim myCell As Range 

    If IsNumeric(Target.Value) Then 'Only run if the a number was entered 

     Set rngPriorityList = ThisWorkbook.Names("priority").RefersToRange 'the named range for the task list 

     If Not Intersect(Target, rngPriorityList) Is Nothing Then 'Only run the following in the cell being updated was in the priority list range 
      For Each myCell In rngPriorityList.Cells 'Loop through the priority list range 
       If myCell.Value = Target.Value _ 
       And myCell.Address <> Target.Address Then 'Finding cells with the same value, excluding the cell being changes 
        myCell.Value = myCell.Value + 1 'Increment the prioriry by 1 
       End If 
      Next myCell 
     End If 
    End If 
End Sub 
+0

它可以工作,但在一个小列表(仅包含6个项目)上进行测试后,值会更新,然后Excel会挂起几秒钟,然后我才能执行任何操作。这是正常的吗? – 2013-05-13 15:39:35

+0

您是否已将其添加到存储您的任务列表的工作表的vba代码?你有没有更新它,以获得rngPriorityList的正确范围? – 2013-05-13 15:41:50

+0

在更长的列表中(例如最多30个),它会挂起Excel并转到“未响应”近一分钟。不知道它是代码中的东西还是我的结尾。我以前从未有过Excel。 – 2013-05-13 15:45:54

1

我会倾向于一个VBA的答案,但我发现公式和列的添加做你需要没有宏。 查看图片

为了保持这项工作从一个新增加到另一个,在添加每个新任务之后,您必须在E:F列中复制已排序的任务列表,然后删除B2和C2中的值。然后,您需要将列表拖到C:F列底部,因为列表只是变长了。

这是我可以看到在单元格公式中做的最大的问题,更新需要复制粘贴删除。

Dynamically Change a numbered list based on a Set Priority Formulas Dynamically Change a numbered list based on a Set Priority Results

+0

我认为它可以在公式单元格中完成,但复制是我想避免的。使用它的人不会做所有这些步骤,也不会遵循方向。他们想要输入,保存并完成。 (宠坏了!) – 2013-05-13 16:03:10

+0

@scheballs +1这种替代方法 - 通常更好的做法来解决使用内置功能而不是'vba'的xl问题 – whytheq 2014-09-18 13:17:18