简体   繁体   English

根据用户动态更改值分配优先级 excel vba

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

I have list courses in cell b and their respective priorities in cell c from 1 to 49. what I want is if a user changes any value of the priority column ie "C".我在单元格 b 中列出了课程,在单元格 c 中列出了它们各自的优先级,从 1 到 49。我想要的是用户是否更改了优先级列的任何值,即“C”。 then all other priority should be adjusted accordingly.那么所有其他优先级都应相应调整。 logic can be seen in the attached sheet.逻辑可以在附表中看到。 the priority numbers should change dynamically as the user enters the value.当用户输入值时,优先级数字应该动态变化。 so in example one referring column L in the attached sheet.所以在示例中,附表中的一个引用列 L。 if user change the no 4 priority to 8 then the rest will go one down .如果用户将第 4 个优先级更改为 8,那么其余的将下降一个。 similarly now we have got new nos list.同样,现在我们有了新的 nos 列表。 so if any other number changes then it should adjust accordingly,keeping in mind the new list sheet snapshot attached因此,如果任何其他数字发生变化,则应相应地进行调整,请记住附加的新列表表快照

Tried the below code but it always starts with the value 1 again.尝试了下面的代码,但它总是再次以值 1 开头。 So the values are not adjusted based on new list.所以这些值不会根据新列表进行调整。

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

Edited to work when first row is any row当第一行是任何行时编辑工作

The following was generated ...生成了以下...

在此处输入图片说明

from this code ...从这个代码...

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