使用vba计算行数

时间:2016-03-08 14:31:01

标签: excel vba excel-vba

我正在尝试计算包含特定值的行数,有三行,我正在计算每一行的整体值并放入一个变量,以便我以后可以在脚本中打印到屏幕。我只计算某个单元格区域内的行,这是代码的第一部分 - 查找第一个和最后一个出现的位置,以便我可以将它们用作行号

Dim mediumStartRow As Integer
Dim mediumEndRow As Integer
Dim majVarCount As Integer
Dim minVarCount As Integer
Dim onTrackCount As Integer 

'Find the range of cells for Medium Projects, just gives the row number of the first occurnace and the last occurnace
mediumStartRow = Range("H:H").Find(what:="Medium Project", after:=Range("H21")).Row
mediumEndRow = Range("H:H").Find(what:="Medium Project", after:=Range("H21"), searchdirection:=xlPrevious).Row
MsgBox "First and Last Row for Medium Projects: " & mediumStartRow & mediumEndRow

    majVarCount = 0
    minVarCount = 0
    onTrackCount = 0

    With Range("AS:AU" & mediumStartRow)
        If Value = "Major Variance" Then
            majVarCount = Rows.Count
            Else
            If Value = "Minor Variance" Then
                minVarCount = Rows.Count
                Else
                If Value = "On Track" Then
                    onTrackCount = Rows.Count
                End If
            End If
        End If
        Do Until Range("AS:AU" & mediumEndRow)
        Loop
    End With

    MsgBox "Major Project Count: " & majVarCount

上面是我到目前为止的代码,但我似乎得到了运行时错误 - 对象'_Global'的方法'范围'失败 - 在线

With Range("AS:AU" & mediumStartRow)

有关如何修复的任何线索

1 个答案:

答案 0 :(得分:1)

您需要在范围语句中定义第一个和最后一个行号。即。

With Range("AS" & mediumStartRow & ":AU" & mediumEndRow)