2013-05-13 87 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