2017-12-19 6 views
0

列Aに一定の条件がある場合は、列P(パス、危険度、失敗)に値を追加するマクロに興味があります。特定の条件が適用されたときの値の追加

以下のマクロをインスピレーションとして使用できるかどうか疑問です。特定の条件が満たされている場合は、色を付けるために作成されました。

I思いも値の列Pに特定のセルの色を割り当てる新しいマクロ様:失敗(マクロ以下と同じ色)のリスクと赤ATに対するパス、イエローグリーン

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 
+0

を簡素化することができますあなたの質問は何ですか?あなたは何を試しましたか?あなたは何をしていますか? – jmdon

答えて

0

rngSearch"At Risk"ある場合、これは、列Pが黄色になるだろう:

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 

他の人は、それに応じて行われるべきです。

0

はい、あなたは、

Dim i As Long, lngColor as Long 'It is inadvisable to declare variables which could also be the name of built in functions and objects, in your case I would not declare "rows" as a variable as it is also a property of an object 
Dim varVal as Variant 
Dim ws As Worksheet 

Set ws = Thisworkbook.Worksheets("Sheet1") 'As general advice, avoid active and select but used fixed values, this way no confusion can exist as to which sheet is used In my example it is Sheet1, but you have to set it to the actual name of your sheet 

with ws 
    For i = 1 To .UsedRange.Rows.Count 
     Select Case .Cells(i, 1) 'Looks at the value of row i column A, and then below if it matches a case. 
      Case "Unexpected Status" 
       varVal = "Pass" 
       lngColor = 13434828 
      Case "At Risk" 
       varVal = "At Risk" 
       lngColor = 8420607 
      Case "Requirements Definition" 
       varVal = "Failed" 
       lngColor = 10092543 
      Case else 
       varVal = Empty 
       lngColor = 0 
     End Select 
     .Cells(i, 16) = varVal 'row i in column P is set to the value determined by the condition in the select case part 
     If Not lngColor = 0 Then 'This value of lngColor is only present if the select case did not match and if so, the color should not be changed 
      .Range(.Cells(i, 1), .Cells(i, 16)).Interior.Color = lngColor 'Set the range of row i column A to column P to the color specified by the case select. 
     End If 
    Next i 
End With 
+0

ありがとう、SilentRevolution、あなたのマクロは美しく動作します! :-) – DKAbi

関連する問題