首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >移至下一行重复进程vba

移至下一行重复进程vba
EN

Stack Overflow用户
提问于 2016-09-07 01:18:48
回答 3查看 12K关注 0票数 1

我有一个包含A列的工作表,列出了一系列风险。

列F列出了可以缓解这些风险的项目。我想写一个脚本来识别风险是否有控制,例如

代码语言:javascript
复制
A                  F
Risk7              control
                   monitor
                   test
Risk8              (blank)

因此输出将是一个有控制的风险和一个没有控制的风险

到目前为止,我的脚本是

代码语言:javascript
复制
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的新手,虽然我认为这不应该那么难,但我没有取得任何进展。任何正确方向的提示都将不胜感激!谢谢大家,保罗

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-07 04:27:02

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

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

(更新以允许数据中的空行)

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2016-09-07 01:31:01

我提出了这个修正

代码语言:javascript
复制
 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
票数 2
EN

Stack Overflow用户

发布于 2016-09-07 01:32:46

尝试下面的代码,我认为在您的例子中,最好对行使用For循环,扫描Column AColumn F中的值(而不是整个范围)。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39354550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档