excel单元格,根据单元格显示内容" position"列

时间:2015-12-06 12:04:44

标签: excel

我试图根据特定列中单元格的相对位置(行)来更改单元格的值。 举例说明:

Name    Up   Down   NoMove   New
Bob     0    0      1        0     
Fred    0    0      1        0 
Jane    0    0      1        0 
Peter   0    0      1        0 
etc

外部资源将改变人们的立场(鲍勃,弗雷德)。当发生这种情况时,4列应反映相对于先前状态的当前状态

Name    Up   Down   NoMove   New
Fred    1    0      0        0     
Bob     0    1      0        0 
Jane    0    0      1        0 
Alan    1    0      0        1 
etc

我已经遍布Stackoverflow,但找不到任何可行的方法。我甚至不确定它是否可行,但也许我没有正确的术语。有人能告诉我在哪里寻找或提供解决方案吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的代码: -

Sub UpDown()

Dim LastRow As Long
Dim var As Variant
Dim name As String
Dim Position As Long

With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For i = 2 To LastRow

    Range("B:B").Cells(i) = 0
    Range("C:C").Cells(i) = 0
    Range("D:D").Cells(i) = 0
    Range("E:E").Cells(i) = 0

    var = Application.Match(Range("A:A").Cells(i), Range("F:F"), 0)
' Not found
    If (IsError(var)) Then
       Range("E:E").Cells(i) = 1
' Found
    Else
        Position = var
    ' Moved up
        If i < Position Then
            Range("B:B").Cells(i) = 1
        Else
    ' Stayed the same
            If i = Position Then
                Range("D:D").Cells(i) = 1
            Else
        ' Moved down
                Range("C:C").Cells(i) = 1
            End If
        End If
    End If
 Next i

'Copy names

For i = 2 To LastRow
    Range("F:F").Cells(i) = Range("A:A").Cells(i)
Next i

End With

End Sub

我将它分配给一个按钮以便对其进行测试,但您也可以将其设置为更改事件处理程序,以便每次A列中的某些内容发生更改时运行它,或者按照here所述的计时器运行它

开始条件: -

enter image description here

第一遍: -

enter image description here

第二遍: -

enter image description here