[英]VBA that will auto sort a column containing formulas in the cells?
我有一列 (D) 正在計算它旁邊的兩列 (B, C) 的平均值。 當對 B 或 C 進行更改時,我需要按降序對 D 列中的公式結果進行自動排序。 D 列單元格包含公式 =IFERROR(C2/B2,0)
B 和 C 通過添加另一個工作表中的數據的公式進行更新。
a b c d
5L2R 1 0 0%
5R2T 2 0 0%
5P1V 1 1 100%
611Q 1 1 100%
5I31 1 1 100%
5N32 1 1 100%
642O 1 1 100%
我嘗試了其他類似帖子中的各種不同代碼,但沒有任何效果。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("D:D")) Is Nothing Then
Range("D1").Sort Key1:=Range("D2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
邊緣,
您需要提供更多的排序信息,並且您不想檢查 D 列的更改,而是檢查它的輸入所依賴的列(B 和 C)。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lLastRow As Long
If Not Intersect(Target, Range("B:C")) Is Nothing Then
lLastRow = [A1].End(xlDown).Row
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add2 Key:=Range("D1:D" & lLastRow), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.SetRange Range("A1:D" & lLastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
End Sub 'Worksheet_Change
HTH
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.