我有一个包含A列的工作表,列出了一系列风险。
列F列出了可以缓解这些风险的项目。我想写一个脚本来识别风险是否有控制,例如
A F
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的新手,虽然我认为这不应该那么难,但我没有取得任何进展。任何正确方向的提示都将不胜感激!谢谢大家,保罗
发布于 2016-09-07 04:27:02
下面的代码应该检查风险的每个“块”,看看"Control“(不区分大小写)是否出现在块中。
我不确定风险是在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发布于 2016-09-07 01:31:01
我提出了这个修正
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发布于 2016-09-07 01:32:46
尝试下面的代码,我认为在您的例子中,最好对行使用For循环,扫描Column A和Column 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 Subhttps://stackoverflow.com/questions/39354550
复制相似问题