簡體   English   中英

根據用戶動態更改值分配優先級 excel vba

[英]assigning priority based on user dynamically changing values excel vba

我在單元格 b 中列出了課程,在單元格 c 中列出了它們各自的優先級,從 1 到 49。我想要的是用戶是否更改了優先級列的任何值,即“C”。 那么所有其他優先級都應相應調整。 邏輯可以在附表中看到。 當用戶輸入值時,優先級數字應該動態變化。 所以在示例中,附表中的一個引用列 L。 如果用戶將第 4 個優先級更改為 8,那么其余的將下降一個。 同樣,現在我們有了新的 nos 列表。 因此,如果任何其他數字發生變化,則應相應地進行調整,請記住附加的新列表表快照

嘗試了下面的代碼,但它總是再次以值 1 開頭。 所以這些值不會根據新列表進行調整。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myVal As Variant
Dim iCount As Long

Dim cell As Range
Dim myRange As Range
Set myRange = Worksheets("Sheet1").Range("C1:C49")

If Intersect(Target, Range("C1:C49")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False

myVal = Target.Value
iCount = 1
For Each cell In myRange
    If Intersect(Target, cell) Is Nothing Then
        If iCount = myVal Then
            iCount = iCount + 1
        End If
        cell.Value = iCount
        iCount = iCount + 1
    End If
Next cell


Application.EnableEvents = True

End Sub

當第一行是任何行時編輯工作

生成了以下...

在此處輸入圖片說明

從這個代碼...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ExtVal As Variant, InsVal As Variant
Dim iLoop As Long
Dim InsRow As Long, ExtRow As Long
Dim foundArr() As Boolean

Dim myRange As Range

    ' initial settings
    Set myRange = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    ReDim foundArr(1 To myRange.Rows.Count)
    For iLoop = 1 To myRange.Rows.Count
        foundArr(iLoop) = False
    Next iLoop

    If Intersect(Target, myRange) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub

    ' calculate the extracted value - the user entered value
    ExtVal = Target.Value
    ' calculate the inserted value - the number the user typed over
    For iLoop = 1 To myRange.Rows.Count
        foundArr(myRange.Cells(iLoop, 1).Value) = True
    Next iLoop
    For iLoop = 1 To myRange.Rows.Count
        If Not foundArr(iLoop) Then
            InsVal = iLoop
            Exit For
        End If
    Next iLoop

    ' calculate the insertion row - the row the user typed in.
    InsRow = CLng(Right(Target.Address, 1))
    ' calculate the extraction row - the original row of the number the user typed
    ExtRow = 0
    For iLoop = 1 To myRange.Rows.Count
        If myRange.Cells(iLoop, 1).Value = ExtVal And myRange.Cells(iLoop, 1).Row <> InsRow Then
            ExtRow = myRange.Cells(iLoop, 1).Row
            Exit For
        End If
    Next iLoop

    ' do the swap / shuffle
    Application.EnableEvents = False

    For iLoop = myRange.Rows.Count To 1 Step -1
        Debug.Print "Evaluating Row " & myRange.Cells(iLoop, 1).Row
        If (myRange.Cells(iLoop, 1).Row <= ExtRow) Then
            If myRange.Cells(iLoop, 1).Row > InsRow + 1 Then
                myRange.Cells(iLoop, 1).Value = myRange.Cells(iLoop - 1, 1).Value
            Else
                If myRange.Cells(iLoop, 1).Row = InsRow + 1 Then
                    myRange.Cells(iLoop, 1).Value = InsVal
                End If
            End If
        End If
    Next iLoop

    Application.EnableEvents = True
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM