移动到下一行重复过程vba

我有一个列A列出一系列风险的工作表。

列F列出了可以减轻这些风险的项目。 我想编写一个脚本来确定一个控制中的风险,或者没有控制的风险

AF Risk7 control monitor test Risk8 (blank) 

所以产出将是一个风险与一个控制和一个风险没有

我的脚本到目前为止

 Sub CountWithWithoutControls() ' counts number of risks with controls and without controls, ' currently running through each cell and comparing back to original control, ' need to write to get it to walk down the series Dim cell As Range Dim myrange As Range Dim control As Long Dim WoControl As Long Set myrange = Range("a7:f27") For Each cell In myrange If Range("c7") <> "" And Range("f7") = "Control" Then control = control + 1 If Range("c7") <> "" And Range("f7") <> "Control" Then WoControl = WoControl + 1 Next cell MsgBox control & " = number of risks with controls" & WoControl & (" = number of risks without controls") End Sub 

我是VBA的新手,虽然我不认为这应该很难,但我没有取得任何进展。 任何提示正确的方向将不胜感激! 谢谢大家保罗

下面的代码应该检查每个“块”的风险,看看“控制”(区分大小写)是否出现在块内。

我不确定风险是在列A还是列C(似乎在问题和其他答案中都提到),所以我使用常量来定义列和开始行 – 适当地更改它们。

(已更新,允许在数据中留空行)

 Sub CountWithWithoutControls() Const RiskColumn As String = "C" Const ControlColumn As String = "F" Const StartAtRow As Long = 7 Dim r As Long Dim ControlFound As Boolean Dim control As Long Dim WoControl As Long Dim lastRow As Long With ActiveSheet lastRow = .Range(RiskColumn & .Rows.Count).End(xlUp).Row r = .Range(ControlColumn & .Rows.Count).End(xlUp).Row If lastRow < r Then lastRow = r End If ControlFound = False For r = StartAtRow To lastRow If UCase(Trim(CStr(.Cells(r, ControlColumn).Value))) = "CONTROL" Then ControlFound = True End If If Not IsEmpty(.Cells(r + 1, RiskColumn)) Then 'Store info for previous block each time we encounter a new "risk" If ControlFound Then control = control + 1 Else WoControl = WoControl + 1 End If ControlFound = False End If Next 'Store info for final "block" If ControlFound Then control = control + 1 Else WoControl = WoControl + 1 End If End With MsgBox control & " = number of risks with controls, " & WoControl & " = number of risks without controls" End Sub 

我提出这个更正

  For Each cell In range("F7:F27") if cell.offset(,-3)<>"" then ' check if cell in column C is not empty if cell = "Control" then control=control+1 else WoControl=WoControl+1 end if Next cell 

试试下面的代码,我认为在你的情况下,最好在行上使用For循环,扫描列A列F中的值(而不是整个范围)。

 Sub CountWithWithoutControls() ' counts number of risks with controls and without controls, ' currently running through each cell and comparing back to original control, ' need to write to get it to walk down the series Dim cell As Range 'Dim myrange As Range Dim control As Long Dim WoControl As Long Dim lRow As Long Dim LastRow As Long 'Set myrange = Range("A7:F27") ' find last row in Column A - risks LastRow = Cells(Rows.Count, "A").End(xlUp).Row For lRow = 7 To LastRow If Cells(lRow, "A") <> "" And Cells(lRow, "F") = "Control" Then control = control + 1 If Cells(lRow, "A") <> "" And Cells(lRow, "F") <> "Control" Then WoControl = WoControl + 1 Next lRow MsgBox control & " = number of risks with controls; " & WoControl & " = number of risks without controls" End Sub