VBA初学者 - 在一行中搜索特定值然后对其进行计数

时间:2016-03-09 09:09:35

标签: excel vba excel-vba

我对VBA比较新,我正在学习,所以我的脚本可能不是解决手头问题的最佳方法。我有一系列单元格,我需要检查一行三列是否出现字符串/值,如果它在三列的行中至少出现一次,那么我需要添加一个计数,如果不是那么我需要查找下一个选项,这是另一个值,如果出现多次计算此次出现,然后最后检查最后一个选项并为其添加计数。我想将计数打印到工作表中的单元格。

不幸的是我不知道我将要搜索的第一行和最后一行是什么,所以我有一些脚本会告诉我这些并分配给一个整数,我知道这有效。什么不起作用是我搜索行并查找值并添加计数的第二部分。它确实打印到工作表,但我得到的只是三个0。

我不确定我的代码在哪里出错 - 编译好没有错误。

这是我的脚本: -

Public Sub counts()

    Dim redVarCount As Integer
    Dim amberVarCount As Integer
    Dim greenVarCount As Integer

    'Used to find the first and last occurrence of the type want to count colors for
    mediumStartRow = Range("H:H").Find(what:="Medium", after:=Range("H21")).Row
    mediumEndRow = Range("H:H").Find(what:="Medium", after:=Range("H21"), searchdirection:=xlPrevious).Row
    MsgBox "First and Last Row for Medium Projects: " & mediumStartRow & mediumEndRow

    redVar = "Red Variance" 'This is the value I am looking for in the row - Red Variance
    amberVar = "Amber Variance" 'this is the value I am looking for in the row - Amber Variance
    greenVar = "Green Variance" 'This is the value I am looking for in the row - Green Variance

    redVarCount = 0
    amberVarCount = 0
    greenVarCount = 0

    For Each Row In Range("AS" & mediumStartRow & ":AU" & mediumEndRow)
        If Row.Value2 = redVar > 0 Then
            redVarCount = redVarCount + 1
            Else
            If Row.Value2 = amberVar > 0 Then
                amberVarCount = amberVarCount + 1
                Else
                If Row.Value2 = greenVar > 0 Then
                    greenVarCount = greenVarCount + 1
                End If
            End If
        End If
    Next

    Range("DO22").Value = redVarCount
    Range("DO23").Value = amberVarCount
    Range("DO24").Value = greenVarCount

End Sub

任何人都可以告诉我出错的地方或更好的方法吗

编辑1

以下是数据的示例

Medium  Green   Red Amber       Counts for medium   
medium  Green   Green   Green       Green   1
medium  Amber   Green   Green       Amber   1
large   Green   Amber   Amber       Red 1
large   Red Green   Green           
small   Amber   Amber   Amber           
small   Green   Green   Green           
small   Green   Green   Amber           
small   Green   Amber   Green   

一旦我能够为媒体工作,我就会为大小做这件事。

1 个答案:

答案 0 :(得分:0)

计算放入DO22:DO24的每个变量的出现次数的简单方法是简单地将COUNTIF - 函数放入其中:

Dim sAddress As String
Set sAddress = "AS" & mediumStartRow & ":AU" & mediumEndRow
(...)
Range("DO22").Formula = "=COUNTIF(" & sAddress & "," & redVar & ")"
Range("DO23").Formula = "=COUNTIF(" & sAddress & "," & amberVar & ")"
Range("DO24").Formula = "=COUNTIF(" & sAddress & "," & greenVar & ")"

如果您希望单元格是数字而不是公式,您也可以在宏内部使用工作表函数:

Dim r As Range
Dim redVar As String

Set r = Range("AS" & mediumStartRow & ":AU" & mediumEndRow)
redVar = "Red Variance"
redVarCount = Application.WorksheetFunction.CountIf(r, redVar)

etc.

如果您想要查看原始代码,我肯定认为可以使用一些改进,请在评论中自述。因为我不想在没有源数据的情况下尝试追踪你的错误,而且你的代码有点令人费解,除非你真的想知道你哪里出错了。

- 编辑 - 在稍微扩展您的问题之后,很明显上述解决方案将无效。我把以下的宏一起攻击,这不是很优雅,但应该完成工作。如果您有任何疑问,请与我联系。

Option Explicit

Public Sub counts()
  Dim redVarCount   As Long
  Dim amberVarCount As Long
  Dim greenVarCount As Long
  Dim i             As Long
  Dim mediumStart   As Range
  Dim mediumEnd     As Range
  Dim r             As Range
  Dim redVar        As String
  Dim amberVar      As String
  Dim greenVar      As String
  Dim amberFound    As Boolean
  Dim greenFound    As Boolean


  'Used to find the first and last occurrence of the type want to count colors for
  Set mediumStart = Range("H:H").Find(What:="Medium", After:=Range("H20"), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False)
  Set mediumEnd = Range("H:H").Find(What:="Medium", After:=Range("H20"), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious, MatchCase:=False)

  If Not mediumStart Is Nothing Then
    MsgBox "First and Last Row for Medium Projects: " & mediumStart.Row & " & " & mediumEnd.Row

    redVar = "Red Variance" 'This is the value I am looking for in the row - Red Variance
    amberVar = "Amber Variance" 'this is the value I am looking for in the row - Amber Variance
    greenVar = "Green Variance" 'This is the value I am looking for in the row - Green Variance

    redVarCount = 0
    amberVarCount = 0
    greenVarCount = 0
    amberFound = False
    greenFound = False

    For Each r In Range("AS" & mediumStart.Row & ":AS" & mediumEnd.Row)
      For i = 0 To 2
        Debug.Print r.Offset(0, i).Address
        If r.Offset(0, i).Value2 = redVar Then
          redVarCount = redVarCount + 1
          Exit For
        ElseIf r.Offset(0, i).Value2 = amberVar And Not amberFound Then
          amberVarCount = amberVarCount + 1
          amberFound = True
        ElseIf r.Offset(0, i).Value2 = greenVar And Not amberFound And Not greenFound Then
          greenVarCount = greenVarCount + 1
          greenFound = True
        End If
      Next i
    Next r

    Range("DO22").Value = redVarCount
    Range("DO23").Value = amberVarCount
    Range("DO24").Value = greenVarCount
  End If
End Sub

<强>结果 enter image description here