我會做這樣的(不與COUNTIF):
Sub countCond()
Dim cond As String
Dim i As Long
Dim countCond As Long
Dim column As Variant
cond = "No" 'You can set this to 'Yes' as well
With Sheet1 'Or Worksheets("Sheet1") 'or whatever you have
For i = 1 To .Range("TALLY").Cells.Count
'I suggest naming it (at least the sum column)
'--Edit: Without the header, so just the values.
For Each column In .UsedRange.Columns
'Or maybe instead UsedRange, name the section
'that contains these columns
If column.Cells(1).Value Like "*COMP" Then 'Assuming this is true
If column.Cells(i + 1) = cond Then countCond = countCond + 1
End If
Next
.Range("TALLY").Cells(i, 1).Value = countCond
countCond = 0
Next i
End With
End Sub
這樣一來,你的代碼中手工計算每行的COND變量爲每一個看起來像* COMP列。
希望它有幫助。
謝謝你,但我希望做一個'對於Each'在VBA我可以添加到現有的宏,這樣它可以幫助我詳細瞭解Visual Basic的功能。 – bcwhite1618 2014-10-20 15:08:00
達喔錯過了那部分。 – xQbert 2014-10-20 16:26:51