2016-07-29 42 views
1

這裏是我的Excel宏編碼,我需要檢查前兩行任何值都小於15它說第三行「通過」。現在我已經做到了,但它只能工作一行。但我必須檢查明智的整個行和列,我怎麼能實現這一點。你們幫我Excel宏檢查前兩行值小於15

Dim result As String 
Dim score As Integer 
Dim score1 As Integer 

Sub wewew() 

score = Range("A1").Value 
score1 = Range("B1").Value 
If score < 15 Or score1 < 15 Then result = "pass" 

Range("C1").Value = result 
    Range("C1").Interior.Color = RGB(255, 0, 0) 

End Sub 

回答

3

非VBA路

要在單元格C1這個公式拉下來

=IF(OR(A1<15,B1<15),"Pass","") 

然後用首頁 | 條件格式以色列C

VBA路

Sub Sample() 
    Dim ws As Worksheet 
    Dim lRow As Long, i As Long 

    Set ws = Sheet1 '<~~ Set this to the relevant worksheet 

    With ws 
     lRow = .Range("A" & .Rows.Count).End(xlUp).Row '<~~ Find Last Row 

     For i = 1 To lRow 
      If .Range("A" & i).Value < 15 Or .Range("B" & i).Value < 15 Then 
       With .Range("C" & i) 
        .Value = "Pass" 
        .Interior.Color = RGB(255, 0, 0) 
       End With 
      End If 
     Next i 
    End With 
End Sub 
+0

亞洲時報Siddharth潰敗非常感謝傢伙:)它的工作:)你救了我很多:)謝謝:) –

+1

@pnuts:去過忙着項目...可能要儘快回去:D –

+0

ohh好兄弟:)無論如何,你救了我很多。我掙扎超過5小時以上,現在我不得不放鬆我的椅子:) –