在某些条件适用时添加值

时间:2017-12-19 10:35:29

标签: excel vba

我感兴趣的是一个宏,如果A列有特定条件,它会在P列(通过,有风险或失败)中添加一个值 - 见下面的例子。

我想知道下面的宏是否可以用作灵感。如果满足某些条件,则创建它以对行着色。

我还希望新的宏在P列中为某些单元格颜色指定值:绿色表示通过,黄色表示风险,红色表示失败(颜色与下面的宏相同)

Option Explicit

Sub Stackoverflow()

Dim ws As Worksheet
Dim rows As Long, i As Long
Dim rngSearch As Range, rngColor As Range

Application.ScreenUpdating = False
Application.EnableEvents = False

Set ws = ActiveSheet

rows = ws.UsedRange.rows.Count

For i = 1 To rows
Set rngSearch = ws.Cells(i, 1)
Set rngColor = ws.Range("A" & i, "O" & i)

If rngSearch = "Unexpected Status" Then
    rngColor.Interior.Color = 13434828
End If
If rngSearch = "At Risk" Then
    rngColor.Interior.Color = 8420607
End If
If rngSearch = "Requirements Definition" Then
    rngColor.Interior.Color = 10092543
End If

Next i

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

2 个答案:

答案 0 :(得分:0)

如果PrngSearch,这会使列"At Risk"变为黄色:

For i = 1 To rows
    Set rngSearch = ws.Cells(i, 1)
    Set rngColor = ws.Range("A" & i, "O" & i)

    If rngSearch = "Unexpected Status" Then
        rngColor.Interior.Color = 13434828
    End If
    If rngSearch = "At Risk" Then
        Cells(rows, "P").Interior.Color = vbYellow

    End If
    If rngSearch = "Requirements Definition" Then
        rngColor.Interior.Color = 10092543
    End If

Next i

其他人应相应制作。

答案 1 :(得分:0)

是的,你可以,简化

import pytest

@pytest.fixture(scope = 'module')
def global_data():
    return {'presentVal': 0}

@pytest.mark.parametrize('iteration', range(1, 6))
def test_global_scope(global_data, iteration):

    assert global_data['presentVal'] == iteration - 1
    global_data['presentVal'] = iteration
    assert global_data['presentVal'] == iteration